统一返回结果的对象

其实就是一个普通的类,然后定义几个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 @ 2024-02-22 17:25  多多指教~  阅读(5)  评论(0编辑  收藏  举报