一、主要功能
- 响应消息统一
- 异常信息统一处理和自定义异常
二、前置条件
无
三、代码示例
1. ApiResult 类
响应模板类,作为前后端数据响应的模板,包括成功状态,响应码,响应消息,响应数据构成。
public class ApiResult<T> {
private static final String SUCCESS_CODE = "200";
private static final String FAIL_CODE = "500";
private Boolean success;
private String code;
private String message;
private T data;
public ApiResult() {}
public ApiResult(Boolean success, String code) {
this.success = success;
this.code = code;
}
public ApiResult(Boolean success, String code, String message) {
this(success, code);
this.message = message;
}
public ApiResult(Boolean success, String code, String message, T data) {
this(success, code, message);
this.data = data;
}
public static ApiResult<String> fail() {
return new ApiResult<>(false, FAIL_CODE, "fail");
}
public static ApiResult<String> fail(String message) {
return new ApiResult<>(false, FAIL_CODE, message);
}
public static ApiResult<String> fail(String code, String message) {
return new ApiResult<>(false, code, message);
}
public static <T> ApiResult<T> success(T data) {
return new ApiResult<T>(true, SUCCESS_CODE, "success", data);
}
public static ApiResult<String> success() {
return new ApiResult<>(true, SUCCESS_CODE, "success");
}
}
2. ApiException 类
异常处理类,继承RuntimeException,作为程序逻辑异常后的处理
public class ApiException extends RuntimeException {
private static final String DEFAULT_FAIL_CODE = "555";
private String errorCode = DEFAULT_FAIL_CODE;
public ApiException() {
}
public ApiException(String message) {
super(message);
}
public ApiException(String errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public ApiException(String message, Throwable cause, String errorCode) {
super(message, cause);
this.errorCode = errorCode;
}
public ApiException(Throwable cause, String errorCode) {
super(cause);
this.errorCode = errorCode;
}
public ApiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String errorCode) {
super(message, cause, enableSuppression, writableStackTrace);
this.errorCode = errorCode;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
3. ApiExceptionHandler类
实现注解接口@ControllerAdvice,对程序中的异常的处理,返回给前端信息。
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
@ControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(ApiException.class)
@ResponseBody
public ApiResult<String> apiExceptionHandler(ApiException e) {
return ApiResult.fail(e.getErrorCode(), e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ApiResult<String> methodExceptionHandler(MethodArgumentNotValidException e) {
return ApiResult.fail(e.getMessage());
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ApiResult<String> illegalExceptionHandler(IllegalArgumentException e) {
return ApiResult.fail(e.getMessage());
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseBody
public ApiResult<String> uploadExceptionHandler(MaxUploadSizeExceededException e) {
return ApiResult.fail(e.getMessage());
}
@ExceptionHandler(Exception.class)
@ResponseBody
public ApiResult<String> otherExceptionHandler(Exception e) {
return ApiResult.fail(e.getMessage());
}
}
本人萌新一枚,才疏学浅,如有不足之处和错误的地方,还望各位大神指出。
欢迎大家评论讨论,一起进步。