|
@@ -29,6 +29,7 @@ import org.hswebframework.web.validate.ValidationException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.validation.BindException;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
@@ -112,6 +113,19 @@ public class RestControllerExceptionTranslator {
|
|
.result(errorResults);
|
|
.result(errorResults);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @ExceptionHandler(BindException.class)
|
|
|
|
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
+ ResponseMessage handleException(BindException e) {
|
|
|
|
+ SimpleValidateResults results = new SimpleValidateResults();
|
|
|
|
+ e.getBindingResult().getAllErrors()
|
|
|
|
+ .stream()
|
|
|
|
+ .filter(FieldError.class::isInstance)
|
|
|
|
+ .map(FieldError.class::cast)
|
|
|
|
+ .forEach(fieldError -> results.addResult(fieldError.getField(), fieldError.getDefaultMessage()));
|
|
|
|
+
|
|
|
|
+ return ResponseMessage.error(400, results.getResults().isEmpty() ? e.getMessage() : results.getResults().get(0).getMessage()).result(results.getResults());
|
|
|
|
+ }
|
|
|
|
+
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
ResponseMessage handleException(MethodArgumentNotValidException e) {
|
|
ResponseMessage handleException(MethodArgumentNotValidException e) {
|