接口返回值统一处理工具类

添加了Swagger的注解

/**
 * 接口返回数据格式
 *
 * @param <T>
 * @author 60974
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

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

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

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

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

    public Result() {

    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.ERROR_500;
        this.success = false;
        return this;
    }

    public Result<T> success() {
        this.message = "成功";
        this.code = CommonConstant.OK_200;
        this.success = true;
        return this;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.OK_200;
        this.success = true;
        return this;
    }


    public static Result<Object> ok() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.OK_200);
        r.setMessage("成功");
        return r;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.OK_200);
        r.setMessage(msg);
        return r;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.OK_200);
        r.setResult(data);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(CommonConstant.ERROR_500, msg);
    }

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

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(CommonConstant.NO_AUTHZ, msg);
    }

    /**
     * 未获取到用户信息
     */
    public static Result<Object> nologin(String msg) {
        return error(CommonConstant.NO_LOGIN, msg);
    }



接口返回值状态码常量

public interface CommonConstant {
    /**
     * 正常状态
     */
    public static final Integer STATUS_NORMAL = 0;
    
    /**
     * 禁用状态
     */
    public static final Integer STATUS_DISABLE = -1;
    
    /**
     * 删除标志
     */
    public static final Integer DEL_FLAG_1 = 1;
    
    /**
     * 未删除
     */
    public static final Integer DEL_FLAG_0 = 0;
    
    /**
     * 系统日志类型: 登录
     */
    public static final int LOG_TYPE_1 = 1;
    
    /**
     * 系统日志类型: 操作
     */
    public static final int LOG_TYPE_2 = 2;
    
    /**
     * 用户的serviceSystemId SESION
     */
    public static final String SESSION_SERVICE_SYSTEM = "SESSION_SERVICE_SYSTEM";
    
    /** {@code 500 Server Error} (HTTP/1.0 - RFC 1945) */
    public static final Integer ERROR_500 = 500;
    
    /** {@code 200 OK} (HTTP/1.0 - RFC 1945) */
    public static final Integer OK_200 = 200;
    
    /** 访问权限认证未通过 510 */
    public static final Integer NO_AUTHZ = 510;

    /** 访问权限认证未通过 510 */
    public static final Integer NO_LOGIN= 401;
    
    /** 登录用户令牌缓存KEY前缀 */
    public static final int TOKEN_EXPIRE_TIME = 3600; // 3600秒即是一小时
    
    public static final String PREFIX_USER_TOKEN = "PREFIX_USER_TOKEN_";
    
    /** 字典翻译文本后缀 */
    public static final String DICT_TEXT_SUFFIX = "_dictText";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨洋阳和羊羊羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值