码迷,mamicode.com
首页 > 编程语言 > 详细

java统一结果集封装

时间:2021-02-27 13:00:59      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pack   ring   iba   amp   ret   返回   封装   alibaba   err   

统一返回结果集封装


package com.example.springbootlearn.common;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: fjh
 * @create: 2021-02-26 11:34
 * <p>
 * 统一结果返回
 */
@Data
public class R<T> implements Serializable {

    private String code;

    private String msg;

    private T data;

    static JSONObject emptyJson = new JSONObject();


    public static <T> R ok() {
        return ok(ResultEnum.SUCCESS.code, ResultEnum.SUCCESS.msg);
    }

    public static <T> R ok(String msg) {
        return ok(ResultEnum.SUCCESS.code, msg);
    }

    public static <T> R ok(T data) {
        return ok(ResultEnum.SUCCESS.code, ResultEnum.SUCCESS.msg, data);
    }

    public static <T> R ok(String code, String msg) {
        return ok(code, msg, emptyJson);
    }

    public static <T> R ok(String code, String msg, T data) {
        return info(code, msg, data);
    }

    public static <T> R error() {
        return error(ResultEnum.FAILURE.code, ResultEnum.FAILURE.msg);
    }

    public static <T> R error(String msg) {
        return error(ResultEnum.FAILURE.code, msg);
    }

    public static <T> R error(T data) {
        return error(ResultEnum.FAILURE.code, ResultEnum.FAILURE.msg, data);
    }

    public static <T> R error(String code, String msg) {
        return error(code,msg,emptyJson);
    }

    public static <T> R error(String code, String msg, T data) {
        return info(code,msg,data);
    }

    public static <T> R info(String code, String msg, T data) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        if (null == data) {
            r.setData(emptyJson);
        } else if (data instanceof String) {
            List<String> list = new ArrayList<>();
            list.add(data.toString());
            r.setData(list);
        }
        return r;
    }


    public static enum ResultEnum {
        SUCCESS("200", "操作成功"),
        FAILURE("-1", "操作失败");

        public String code;
        public String msg;

        ResultEnum(String code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }

    public R() {
    }


    public R(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

java统一结果集封装

标签:pack   ring   iba   amp   ret   返回   封装   alibaba   err   

原文地址:https://www.cnblogs.com/fjh0512/p/14452226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!