统一返回结果的对象

其实就是一个普通的类,然后定义几个static静态方法,这样就直接调用该类的static方法。

 

复制代码
package org.ongoal.common;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
@Data
@Slf4j
@Accessors(chain = true)
public class ResponseResult implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String msg;

    private Object data;

    public static ResponseResult success(int code,String msg,Object data){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code).setMsg(msg).setData(data);
        return responseResult;
    }

    public static ResponseResult success(Object data){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200).setMsg("").setData(data);
        return responseResult;
    }

    public static ResponseResult success(){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200).setMsg("请求成功").setData(null);
        return responseResult;
    }

    public static ResponseResult success(int code,String msg){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code).setMsg(msg).setData(null);
        return responseResult;
    }



    public static ResponseResult fail(int code,String msg,Object data){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code).setMsg(msg).setData(data);
        return responseResult;
    }


    public static ResponseResult fail(int code,String msg){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(code).setMsg(msg).setData(null);
        return responseResult;
    }

    public static ResponseResult fail(){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(-1).setMsg("请求失败").setData(null);
        return responseResult;
    }

}
复制代码

 

posted @   多多指教~  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示