1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| @Slf4j @RestControllerAdvice public class ExceptionController {
@ExceptionHandler(value = MethodArgumentNotValidException.class) public ApiMessage<Object> methodArgumentNotValidHandler(MethodArgumentNotValidException ex) { List<ArgumentInvalid> 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<Object> bindExceptionHandler(BindException ex) { List<ArgumentInvalid> 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<Object> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) { log.error("HTTP请求方式不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = MissingServletRequestParameterException.class) public ApiMessage<Object> missingServletRequestParameterException(MissingServletRequestParameterException ex) { log.error("请求参数不全:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = TypeMismatchException.class) public ApiMessage<Object> typeMismatchException(TypeMismatchException ex) { log.error("请求参数类型不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = DataFormatException.class) public ApiMessage<Object> dataFormatException(DataFormatException ex) { log.error("数据格式不正确:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = IllegalArgumentException.class) public ApiMessage<Object> illegalArgumentException(IllegalArgumentException ex) { log.error("非法输入或断言错误:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = ConstraintViolationException.class) public ApiMessage<Object> constraintViolationException(ConstraintViolationException ex) { log.error("请求参数错误:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(value = DataAccessException.class) public ApiMessage<Object> dataDoException(DataAccessException ex) { log.error("操作数据库出现异常:【{}】", ex.getMessage()); return new ApiMessage<>(ex); }
@ExceptionHandler(Exception.class) public ApiMessage<Object> apiExceptionHandler(Exception ex) { int count = 1; StringBuilder sb = new StringBuilder(); for (StackTraceElement stackTraceElement : ex.getStackTrace()) { sb.append(stackTraceElement.toString());
sb.append("\n"); } log.error("系统异常:【{}】", sb.toString()); return new ApiMessage<>(ex); }
@ExceptionHandler(ApiException.class) public ApiMessage<Object> 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; } }
|