5、常用到的注解列表-异常处理
1、@ControllerAdvice、@ExceptionHandler
@ControllerAdvice:注解定义全局异常处理类。
@ExceptionHandler:注解声明异常处理方法。
新建异常信息实体类,非必要的类,主要用于包装异常信息。
public class ErrorResponse {
private String message;
private String errorTypeName;
public ErrorResponse(Exception e) {
this(e.getClass().getName(), e.getMessage());
}
public ErrorResponse(String errorTypeName, String message) {
this.errorTypeName = errorTypeName;
this.message = message;
}
//省略get/set方法
}自定义异常
public class ResourceNotFoundException extends RuntimeException {
private String message;
public ResourceNotFoundException() {
super();
}
public ResourceNotFoundException(String message) {
super(message);
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}异常处理类
要在类上加上@ControllerAdvice注解这个类就成为了全局异常处理类,当然你也可以通过assignableTypes指定特定的Controller类,让异常处理类只处理特定类抛出的异常。
@ControllerAdvice(assignableTypes = {ExceptionController.class})
@ResponseBody
public class GlobalExceptionHandler {
ErrorResponse illegalArgumentResponse = new ErrorResponse(new IllegalArgumentException("参数错误!"));
ErrorResponse resourseNotFoundResponse = new ErrorResponse(new ResourceNotFoundException("Sorry, the resourse not found!"));
// 拦截所有异常,一般情况下一个方法特定处理一种异常
@ExceptionHandler(value = Exception.class)
public ResponseEntity<ErrorResponse> exceptionHandler(Exception e) {
if (e instanceof IllegalArgumentException) {
return ResponseEntity.status(400).body(illegalArgumentResponse);
} else if (e instanceof ResourceNotFoundException) {
return ResponseEntity.status(404).body(resourseNotFoundResponse);
}
return null;
}
}2、全局异常处理
ErrorCode这个枚举类的主要作用就是统一管理系统中可能出现的异常。枚举值可以提供多个,根据不同枚举定位成不同类型的异常,方便前端拿到对应错误码处理。
public enum ErrorCode {
RESOURCE_NOT_FOUND(1001, HttpStatus.NOT_FOUND, "未找到该资源!"),
REQUEST_VALIDATION_FAILED(1002, HttpStatus.BAD_REQUEST, "请求数据格式验证失败!");
privatefinalint code;
privatefinal HttpStatus status;
privatefinal String message;
ErrorCode(int code, HttpStatus status, String message) {
this.code = code;
this.status = status;
this.message = message;
}
public int getCode() {
return code;
}
public HttpStatus getStatus() {
return status;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return"ErrorCode{" +
"code=" + code +
", status=" + status +
", message='" + message + '\'' +
'}';
}
}这个类作为异常信息返回给客户端,里面包括了当出现异常时我们想要返回给客户端的所有信息。
public class ErrorReponse {
private int code;
private int status;
private String message;
private String path;
private Instant timestamp;
private HashMap<String, Object> data = new HashMap<String, Object>();
public ErrorReponse() {
}
public ErrorReponse(BaseException ex, String path) {
this(ex.getError().getCode(), ex.getError().getStatus().value(), ex.getError().getMessage(), path, ex.getData());
}
public ErrorReponse(int code, int status, String message, String path, Map<String, Object> data) {
this.code = code;
this.status = status;
this.message = message;
this.path = path;
this.timestamp = Instant.now();
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}
// 省略 getter/setter 方法
@Override
public String toString() {
return"ErrorReponse{" +
"code=" + code +
", status=" + status +
", message='" + message + '\'' +
", path='" + path + '\'' +
", timestamp=" + timestamp +
", data=" + data +
'}';
}
}BaseException自定义顶层异常类,系统异常都要继承自这个类。
public abstract class BaseException extends RuntimeException {
private final ErrorCode error;
private final HashMap<String, Object> data = new HashMap<>();
public BaseException(ErrorCode error, Map<String, Object> data) {
super(error.getMessage());
this.error = error;
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}
protected BaseException(ErrorCode error, Map<String, Object> data, Throwable cause) {
super(error.getMessage(), cause);
this.error = error;
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}
public ErrorCode getError() {
return error;
}
public Map<String, Object> getData() {
return data;
}
}自定义业务异常,利用前面的枚举ErrorCode类,统一管理错误码
public class ResourceNotFoundException extends BaseException {
public ResourceNotFoundException(Map<String, Object> data) {
super(ErrorCode.RESOURCE_NOT_FOUND, data);
}
}全局异常捕获
@ControllerAdvice(assignableTypes = {ExceptionController.class})
@ResponseBody
public class GlobalExceptionHandler {
@ExceptionHandler(BaseException.class)
public ResponseEntity<?> handleAppException(BaseException ex, HttpServletRequest request) {
ErrorReponse representation = new ErrorReponse(ex, request.getRequestURI());
returnnew ResponseEntity<>(representation, new HttpHeaders(), ex.getError().getStatus());
}
@ExceptionHandler(value = ResourceNotFoundException.class)
public ResponseEntity<ErrorReponse> handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
ErrorReponse errorReponse = new ErrorReponse(ex, request.getRequestURI());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorReponse);
}
}版权声明
非特殊说明,本文由Zender原创或收集发布,欢迎转载。
上一篇:4、常用到的注解列表-自动装配相关 下一篇:Spring IOC
ZENDER
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。