统一返回结果类

拿去用吧:

/**
 * @Description: 统一返回结果
 * @Author: xuziguo
 * @Date: 2021/4/17 上午 11:26
 * @Modified By:
 * @Modify Date:
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result implements Serializable {

    private static final long serialVersionUID = -1617443048863070570L;

    //结果状态码
    private String code;

    //响应结果描述
    private String message;

    //返回数据
    private Object data;

    //异常
    private Exception e;

    /**
     * @description: 没有空参构造,openfeign会报错但不影响操作
     * @author: xuziguo
     * @date: 2021/4/23 下午 3:55
     * @param: []
     * @return:
     */
    private Result() {
    }

    /**
     * @description: 状态码,响应结果描述
     * @author: xuziguo
     * @date: 2021/4/17 上午 11:31
     * @param: [code, message]
     * @return:
     */
    private Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * @description: 状态码,响应结果描述,数据
     * @author: xuziguo
     * @date: 2021/4/17 上午 11:31
     * @param: [code, message, data]
     * @return:
     */
    private Result(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * @description: 状态码,响应结果描述,异常
     * @author: xuziguo
     * @date: 2021/5/1 下午 3:05
     * @param: [code, message, e]
     * @return:
     */
    private Result(String code, String message, Exception e) {
        this.code = code;
        this.message = message;
        this.e = e;
    }

    public static Result build(String code, String msg) {
        return new Result(code, msg);
    }

    public static Result build(String code, String msg, Object data) {
        return new Result(code, msg, data);
    }

    public static Result build(String code, String msg, Exception e) {
        return new Result(code, msg, e);
    }

    //get方法必须要有,否则swagger里得不到这些属性
    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    public Exception getException() {
        return e;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值