统一返回值工具类

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。统一返回值工具类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

第一种:

public class ReturnResult {
    private int code = 200;
    private String msg = "success";
    private Object payloads;

    private ReturnResult(){

    }

    public static ReturnResult create() {
        return new ReturnResult();
    }

    public static ReturnResult create(Object payloads) {
        return new ReturnResult().setPayloads(payloads);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ReturnResult setErrorMsg(String msg) {
        this.msg = msg;
        this.code = 500;
        return this;
    }

    public Object getPayloads() {
        return payloads;
    }

    public ReturnResult setPayloads(Object payloads) {
        this.payloads = payloads;
        return this;
    }

}

响应参数

{
  “code”: 200,
  “msg”: “success”,
  “payloads”: [ ]

}

 第二种:



import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.MDC;
import org.springframework.http.HttpStatus;


@Getter
@Setter
@ApiModel(value = "Result", description = "返回结果对象")
public class Result<T> {
    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功!";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T result;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    /**
     * 请求日志跟踪ID
     */
    @ApiModelProperty(value = "日志跟踪ID")
    private  String traceId= MDC.get("TRACE_ID");

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = HttpStatus.OK.value();
        this.success = true;
        return this;
    }

    public static<T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setMessage("成功");
        return r;
    }

    public static<T> Result<T> OK(ReturnCode returnCode) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setMessage(returnCode.getMsg());
        r.setResult(null);
        return r;
    }

    public static<T> Result<T> Error(ReturnCode returnCode) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(returnCode.getCode());
        r.setMessage(returnCode.getMsg());
        r.setResult(null);
        return r;
    }

    public static<T> Result<T> Error(ReturnCode returnCode,T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(returnCode.getCode());
        r.setMessage(returnCode.getMsg());
        r.setResult(data);
        return r;
    }

    public static<T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setResult(data);
        return r;
    }

    public static<T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(HttpStatus.OK.value());
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(HttpStatus.EXPECTATION_FAILED.value(), msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(new Integer(code));
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> error(Integer code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }
    public static Result<Object> error(int code, String msg, Object data) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        r.setResult(data);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = HttpStatus.EXPECTATION_FAILED.value();
        this.success = false;
        return this;
    }
    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), msg);
    }

}

{
  “success”: true,
  “message”: “操作成功!”,
  “code”: 200,
  “result”: “time:0/5 * * * * ?,state:NORMAL”,
  “timestamp”: 1655368892383,
  “traceId”: null
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192854.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!