|
@@ -2,16 +2,18 @@ package com.nokia.config;
|
|
|
|
|
|
import com.nokia.common.R;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.TypeMismatchException;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
+import org.springframework.http.converter.HttpMessageConversionException;
|
|
|
import org.springframework.validation.BindException;
|
|
|
import org.springframework.validation.FieldError;
|
|
|
-import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
+import org.springframework.web.bind.MissingRequestValueException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
-import javax.validation.ConstraintViolationException;
|
|
|
+import javax.validation.ValidationException;
|
|
|
|
|
|
|
|
|
* 请求异常处理
|
|
@@ -20,48 +22,55 @@ import javax.validation.ConstraintViolationException;
|
|
|
@RestControllerAdvice
|
|
|
public class ControllerExceptionHandler
|
|
|
{
|
|
|
- @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
- public ResponseEntity<Object> httpMessageNotReadableExceptionValidatorHandler(HttpMessageNotReadableException e)
|
|
|
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
+ public ResponseEntity<Object> httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e)
|
|
|
{
|
|
|
- log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", e.getMessage());
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(R.error().message(e.getMessage()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(MissingRequestValueException.class)
|
|
|
+ public ResponseEntity<Object> missingRequestValueExceptionHandler(MissingRequestValueException e)
|
|
|
+ {
|
|
|
+ log.warn(e.getMessage());
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
|
|
|
}
|
|
|
|
|
|
- @ExceptionHandler(BindException.class)
|
|
|
- public ResponseEntity<Object> bindExceptionValidatorHandler(BindException e)
|
|
|
+ @ExceptionHandler(HttpMessageConversionException.class)
|
|
|
+ public ResponseEntity<Object> httpMessageConversionExceptionHandler(HttpMessageConversionException e)
|
|
|
{
|
|
|
- FieldError fieldError = e.getBindingResult().getFieldError();
|
|
|
- String message = "";
|
|
|
- if (fieldError != null)
|
|
|
- {
|
|
|
- message = fieldError.getDefaultMessage();
|
|
|
- log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
|
|
|
- }
|
|
|
- return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
|
|
|
}
|
|
|
|
|
|
- @ExceptionHandler(ConstraintViolationException.class)
|
|
|
- public ResponseEntity<Object> constraintViolationExceptionValidatorHandler(ConstraintViolationException e)
|
|
|
+ @ExceptionHandler(TypeMismatchException.class)
|
|
|
+ public ResponseEntity<Object> typeMismatchExceptionHandler(TypeMismatchException e)
|
|
|
{
|
|
|
- String message = e.getConstraintViolations().iterator().next().getMessage();
|
|
|
- log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
|
|
|
- return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
|
|
|
}
|
|
|
|
|
|
- @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
- public ResponseEntity<Object> methodArgumentNotValidExceptionValidatorHandler(MethodArgumentNotValidException e)
|
|
|
+ @ExceptionHandler(BindException.class)
|
|
|
+ public ResponseEntity<Object> bindExceptionHandler(BindException e)
|
|
|
{
|
|
|
FieldError fieldError = e.getBindingResult().getFieldError();
|
|
|
String message = "";
|
|
|
if (fieldError != null)
|
|
|
{
|
|
|
message = fieldError.getDefaultMessage();
|
|
|
- log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
|
|
|
}
|
|
|
+ log.warn(message);
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
|
|
|
}
|
|
|
-
|
|
|
- @ExceptionHandler({Exception.class})
|
|
|
+
|
|
|
+ @ExceptionHandler(ValidationException.class)
|
|
|
+ public ResponseEntity<Object> validationExceptionHandler(ValidationException e)
|
|
|
+ {
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
public ResponseEntity<Object> exceptionHandler(Exception e)
|
|
|
{
|
|
|
log.error("╭( ′• o •′ )╭☞ 发生错误了 {}", e.getMessage(), e);
|