- 明确接口返回,自定义统一的错误码,BaseResponse类,ResultUtils类,ErrorCode枚举类
- 并封装了自定义异常处理器BusinessException, 全局异常处理器GlobalException,规范了异常返回,屏蔽项目冗余的报错细节
对简历中的这点,进行详细说明
1 通用返回类
在Common包下新建BaseResponse类,包含4个属性以及由它们组成的不同构造方法
@Data
public class BaseResponse<T> implements Serializable {
private int code;
private T data;
private String message;
private String description;
public BaseResponse(int code, T data, String message, String description) {
this.code = code;
this.data = data;
this.message = message;
this.description = description;
}
public BaseResponse(int code, T data, String message) {
this(code, data, message, "");
}
public BaseResponse(int code, T data) {
this(code, data, "", "");
}
public BaseResponse(ErrorCode errorCode) {
this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());
}
}
2 返回工具类:ResutUtils,它其实就是对BaseResponse的封装,比如:在返回成功的数据时,
return new BaseResponse<>(0, data, “ok”); 这个是标准写法,但是就怕有的人开发时不注意把“ok”换成其他值,这样就没有实现开发时要统一规范
public class ResultUtils {
//成功
public static <T> BaseResponse<T> success(T data) {
return new BaseResponse<>(0, data, "ok");
}
//失败
public static BaseResponse error(ErrorCode errorCode) {
return new BaseResponse<>(errorCode);
}
//失败,data返回数据为null
public static BaseResponse error(int code, String message, String description) {
return new BaseResponse(code, null, message, description);
}
//失败, 返回自定义错误码中的错误代码
public static BaseResponse error(ErrorCode errorCode, String message, String description) {
return new BaseResponse(errorCode.getCode(), null, message, description);
}
//失败,返回自定义错误码中的信息
public static BaseResponse error(ErrorCode errorCode, String description) {
return new BaseResponse(errorCode.getCode(), errorCode.getMessage(), description);
}
}
3 通过枚举类ErrorCode,自定义错误码,像http中原本的404,服务端错误,错误太宽泛了,所以我们自定义一些错误码,可以在打印日志时,告诉我们具体是哪里有问题
public enum ErrorCode {
SUCCESS(0, "ok", ""),
PARAMS_ERROR(40000, "请求参数错误", ""),
NULL_ERROR(40001, "请求数据为空", ""),
NOT_LOGIN(40100, "未登录", ""),
NO_AUTH(40101, "无权限", ""),
SYSTEM_ERROR(50000, "系统内部异常", "");
private final int code;
/**
* 状态码信息
*/
private final String message;
/**
* 状态码描述(详情)
*/
private final String description;
//构造方法重写
ErrorCode(int code, String message, String description) {
this.code = code;
this.message = message;
this.description = description;
}
//因为只需要获取这些信息,所以只用重写get即可
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public String getDescription() {
return description;
}
}
4 自定义异常类
public class BusinessException extends RuntimeException {
private final int code;
private final String description;
public BusinessException(String message, int code, String description) {
super(message);
this.code = code;
this.description = description;
}
public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
this.description = errorCode.getDescription();
}
public BusinessException(ErrorCode errorCode, String description) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
}
5 只自定义异常,在前端会返回具体的报错信息,所以定义全局异常处理器,捕获代码中的所有异常类集中处理
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public BaseResponse businessExceptionHandler(BusinessException e) {
log.error("businessException: " + e.getMessage(), e);
return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
}
@ExceptionHandler(RuntimeException.class)
public BaseResponse runtimeExceptionHandler(RuntimeException e) {
log.error("runtimeException", e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
}
}