SpringBoot 全局异常处理
@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;
}
}
打开App,阅读手记