2.规定统一全局的状态码{
"data": null, 结果集(出现异常,结果集为空)
"code": 1007, 状态码
"message": "" 状态码的描述
}
如果是单体项目,可以使用一个全局的异常状态码类
如果是微服务架构,可以写一个全局异常状态码类, 每个服务单独再分开写服务的状态码类
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 CommonResult4.如何使用{ 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; } }
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());
}



