springboot 全局统一自定义返回对象

37人浏览 / 0人评论

全局自定义返回对象的优点

  • 整个项目统一的返回格式,如果后期要修改,直接修改公共的model,方便维护
  • 传给前端统一的格式,前端可以做全局处理等等
package blog.util;

/**
 * 全局响应类
 * 东泰 http://dt2008.cn
 * 2020-11
 */
public class Response<T> {

    //是否成功
    private boolean success;

    //状态编码
    private String code;

    //处理结果描述
    private String msg;

    //返回的对象信息
    private T data;

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String isCode() {
        return code;
    }

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

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

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

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success() {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg("success");
        response.setData(null);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg(msg);
        response.setData(null);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(T obj) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg("success");
        response.setData(obj);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(T obj, String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg(msg);
        response.setData(obj);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> error(T obj, String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setMsg(msg);
        response.setData(obj);
        return response;
    }

    /**
     * 处理异常返回
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> error(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setMsg(msg);
        response.setData(null);
        return response;
    }

    /**
     * 处理警告返回
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> warn(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setCode("warn");
        response.setMsg(msg);
        response.setData(null);
        return response;
    }
}

用法

全部评论