🐛 修复类校验失败时,返回信息无法正确获取
This commit is contained in:
@@ -12,7 +12,7 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
import org.springframework.security.core.SpringSecurityMessageSource;
|
import org.springframework.security.core.SpringSecurityMessageSource;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
@@ -20,7 +20,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
|
|
||||||
import javax.validation.ValidationException;
|
import javax.validation.ValidationException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局异常处理
|
* 全局异常处理
|
||||||
@@ -117,17 +116,17 @@ public class GlobalExceptionHandlerResolver {
|
|||||||
@ExceptionHandler({ MethodArgumentNotValidException.class, BindException.class })
|
@ExceptionHandler({ MethodArgumentNotValidException.class, BindException.class })
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
public R<String> handleBodyValidException(Exception exception) {
|
public R<String> handleBodyValidException(Exception exception) {
|
||||||
List<FieldError> fieldErrors;
|
BindingResult bindingResult;
|
||||||
|
|
||||||
if (exception instanceof BindException) {
|
if (exception instanceof BindException) {
|
||||||
BindException bindException = (BindException) exception;
|
bindingResult = ((BindException) exception).getBindingResult();
|
||||||
fieldErrors = bindException.getBindingResult().getFieldErrors();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
MethodArgumentNotValidException e = (MethodArgumentNotValidException) exception;
|
bindingResult = ((MethodArgumentNotValidException) exception).getBindingResult();
|
||||||
fieldErrors = e.getBindingResult().getFieldErrors();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String errorMsg = fieldErrors.get(0).getDefaultMessage();
|
String errorMsg = bindingResult.getErrorCount() > 0 ? bindingResult.getAllErrors().get(0).getDefaultMessage()
|
||||||
|
: "未获取到错误信息!";
|
||||||
|
|
||||||
log.error("参数绑定异常,ex = {}", errorMsg, exception);
|
log.error("参数绑定异常,ex = {}", errorMsg, exception);
|
||||||
globalExceptionHandler.handle(exception);
|
globalExceptionHandler.handle(exception);
|
||||||
|
|||||||
Reference in New Issue
Block a user