全局统一返回类处理

 1.规定统一返回的数据结构

{

        "data": null,             结果集(出现异常,结果集为空)              

        "code": 1007,          状态码

        "message": ""          状态码的描述      

}

2.规定统一全局的状态码

如果是单体项目,可以使用一个全局的异常状态码类

如果是微服务架构,可以写一个全局异常状态码类, 每个服务单独再分开写服务的状态码类

public class ExceptionCode {

    public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
    public static final ExceptionCode FAILED = new ExceptionCode(500, "系统异常");

    private int code;
    private String msg;

    public ExceptionCode(int code, String message) {
        this.code = code;
        this.message= message;
    }

    public ExceptionCode(String message) {
        this.msg = message;
    }

    public Integer getCode() {
        return this.message;
    }

    public String getMessage() {
        return this.message;
    }

}

3.全局处理类

public class CommonResult<T> {
    
    /**
     * 结果
     */
    private T data;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 状态码描述
     */
    private String message;

    public CommonResult() {}

    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    protected CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<T>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMsg());
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ExceptionCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param exceptionCode 错误码
     */
    public static <T> CommonResult<T> failed(ExceptionCode exceptionCode) {
        return new CommonResult<T>(exceptionCode.getCode(), exceptionCode.getMsg(), null);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> CommonResult<T> failed(Integer errorCode, String message) {
        return new CommonResult<T>(errorCode, message, null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ExceptionCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ExceptionCode.FAILED.getMsg());
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ExceptionCode.PARAM_ERROR.getMsg());
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ExceptionCode.PARAM_ERROR.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ExceptionCode.FORBIDDEN.getCode(), ExceptionCode.FORBIDDEN.getMsg(), data);
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

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

}

4.如何使用

1.请求成功,返回集合类型

@ApiOperation(value = "商品品牌列表查询")
@GetMapping("/list")
public CommonResult<List<AdminProductBrandListDTO>> queryList(AdminProductBrandQuery query){
   return CommonResult.success(brandQueryService.queryList(query));
}

2.请求成功,返回Boolean类型

@ApiOperation(value = "商品品牌保存")
@PostMapping("/add")
public CommonResult<Boolean> add(@RequestBody @Validated AdminProductBrandAddCommand command) {
   return CommonResult.success(productBrandApplication.add(command));
}

3.请求失败,返回失败

@ApiOperation(value = "商品品牌保存")
@PostMapping("/add")
public CommonResult<Boolean> add(@RequestBody @Validated AdminProductBrandAddCommand command) {
   // 返回默认错误码和错误描述
   return CommonResult.failed();
   // 返回自定义状态码和错误描述
   return CommonResult.failed(ExceptionCode.FAILED.getCode(), ExceptionCode.FAILED.getMsg()); 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值