@Slf4j @RestControllerAdvice public class ExceptionController { @ExceptionHandler(value = MethodArgumentNotValidException.class) public ApiMessage methodArgumentNotValidHandler(MethodArgumentNotValidException ex) { //按需重新封装需要返回的错误信息 List invalidArguments = new ArrayList<>(); //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息 ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage()))); return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments); } @ExceptionHandler(value = BindException.class) public ApiMessage bindExceptionHandler(BindException ex) { //按需重新封装需要返回的错误信息 List invalidArguments = new ArrayList<>(); //解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息 ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage()))); return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments); } @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) public ApiMessage httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) { log.error("HTTP请求方式不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = MissingServletRequestParameterException.class) public ApiMessage missingServletRequestParameterException(MissingServletRequestParameterException ex) { log.error("请求参数不全:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = TypeMismatchException.class) public ApiMessage typeMismatchException(TypeMismatchException ex) { log.error("请求参数类型不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = DataFormatException.class) public ApiMessage dataFormatException(DataFormatException ex) { log.error("数据格式不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = IllegalArgumentException.class) public ApiMessage illegalArgumentException(IllegalArgumentException ex) { log.error("非法输入或断言错误:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = ConstraintViolationException.class) public ApiMessage constraintViolationException(ConstraintViolationException ex) { log.error("请求参数错误:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(value = DataAccessException.class) public ApiMessage dataDoException(DataAccessException ex) { log.error("操作数据库出现异常:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } @ExceptionHandler(Exception.class) public ApiMessage apiExceptionHandler(Exception ex) { //只打印15行的错误堆栈 int count = 1; StringBuilder sb = new StringBuilder(); for (StackTraceElement stackTraceElement : ex.getStackTrace()) { sb.append(stackTraceElement.toString()); // if (count++ >= 30) { // break; // } sb.append("n"); } log.error("系统异常:【{}】", sb.toString()); return new ApiMessage<>(ex); } @ExceptionHandler(ApiException.class) public ApiMessage apiException(ApiException apiException) { return new ApiMessage<>(apiException); } @Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) static class ArgumentInvalid { private String field; private Object rejectedValue; private String defaultMessage; } }
赵小胖个人博客
上一篇 JDK8新特性、JDK11新特性
下一篇 Spring Boot2 系列教程(十二)@ControllerAdvice 的三种使用场景
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号