@Slf4j @RestControllerAdvice public class ExceptionController { /** * 校驗錯誤攔截處理 * * @param ex 異常 * @return 返回值 * StringBuilder sb = new StringBuilder(); * for (FieldError error : ex.getBindingResult().getFieldErrors()) { * sb.append(error.getDefaultMessage()).append(";"); * } */ @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); } /** * HTTP請求方式不正確 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) public ApiMessage<Object> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) { log.error("HTTP請求方式不正確:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 請求參數不全 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = MissingServletRequestParameterException.class) public ApiMessage<Object> missingServletRequestParameterException(MissingServletRequestParameterException ex) { log.error("請求參數不全:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 請求參數類型不正確 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = TypeMismatchException.class) public ApiMessage<Object> typeMismatchException(TypeMismatchException ex) { log.error("請求參數類型不正確:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 數據格式不正確 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = DataFormatException.class) public ApiMessage<Object> dataFormatException(DataFormatException ex) { log.error("數據格式不正確:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 非法輸入或斷言錯誤 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = IllegalArgumentException.class) public ApiMessage<Object> illegalArgumentException(IllegalArgumentException ex) { log.error("非法輸入或斷言錯誤:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 請求參數錯誤 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = ConstraintViolationException.class) public ApiMessage<Object> constraintViolationException(ConstraintViolationException ex) { log.error("請求參數錯誤:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 操做數據庫出現異常 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(value = DataAccessException.class) public ApiMessage<Object> dataDoException(DataAccessException ex) { log.error("操做數據庫出現異常:【{}】", ex.getMessage()); return new ApiMessage<>(ex); } /** * 系統異常 * * @param ex 異常 * @return 返回值 */ @ExceptionHandler(Exception.class) public ApiMessage<Object> 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); } /** * 自定義異常 * * @param apiException 自定義異常 * @return 返回值 */ @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; } }
趙小胖我的博客java