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());
}