栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-全局统一返回类处理 第一篇

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-全局统一返回类处理 第一篇

 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 {
    
    
    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  CommonResult success() {
        return new CommonResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMsg());
    }

    
    public static  CommonResult success(T data) {
        return new CommonResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMsg(), data);
    }

    
    public static  CommonResult success(T data, String message) {
        return new CommonResult(ExceptionCode.SUCCESS.getCode(), message, data);
    }

    
    public static  CommonResult failed(ExceptionCode exceptionCode) {
        return new CommonResult(exceptionCode.getCode(), exceptionCode.getMsg(), null);
    }

    
    public static  CommonResult failed(Integer errorCode, String message) {
        return new CommonResult(errorCode, message, null);
    }

    
    public static  CommonResult failed(String message) {
        return new CommonResult(ExceptionCode.FAILED.getCode(), message, null);
    }

    
    public static  CommonResult failed() {
        return failed(ExceptionCode.FAILED.getMsg());
    }

    
    public static  CommonResult validateFailed() {
        return failed(ExceptionCode.PARAM_ERROR.getMsg());
    }

    
    public static  CommonResult validateFailed(String message) {
        return new CommonResult(ExceptionCode.PARAM_ERROR.getCode(), message, null);
    }

    
    public static  CommonResult unauthorized(T data) {
        return new CommonResult(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 queryList(AdminProductBrandQuery query){
   return CommonResult.success(brandQueryService.queryList(query));
}

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

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

3.请求失败,返回失败

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号