欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rest接口封装统一返回数据工具类

程序员文章站 2022-06-01 12:57:41
...

 

public class Result {

    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应成功与否
     */
    private boolean success;
    /**
     * 响应消息
     */
    private String msg;
    /**
     * 响应数据
     */
    private Object data;

    public Result() {
    }

    public Result(Integer code, boolean success, String msg) {
        this.code = code;
        this.success = success;
        this.msg = msg;
    }

    public Result(Integer code, boolean success, String msg, Object data) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功 返回默认成功信息
     *
     * @return
     */
    public Result SUCCESS() {
        return new Result(1, true, "操作成功", null);
    }

    /**
     * 成功 返回(data数据)成功信息
     *
     * @param data
     * @return
     */
    public static Result SUCCESS(Object data) {
        return new Result(1, true, "ok", data);
    }

    /**
     * 成功 返回自定义(消息、data数据)成功信息
     *
     * @param msg
     * @param data
     * @return
     */
    public static Result SUCCESS(String msg, Object data) {
        return new Result(1, true, msg, data);
    }

    /**
     * 失败 返回默认失败信息
     *
     * @return
     */
    public static Result ERROR() {
        return new Result(-1, false, "操作失败", null);
    }

    /**
     * 失败 返回自定义(消息)失败信息
     *
     * @param msg
     * @return
     */
    public static Result ERROR(String msg) {
        return new Result(-1, false, msg, null);
    }

    /**
     * 失败 返回自定义(消息、状态码)失败信息
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result ERROR(Integer code, String msg) {
        return new Result(code, false, msg, null);
    }

}

 

相关标签: Java