|
@@ -5,10 +5,13 @@ import cn.com.yusys.yusp.commons.exception.BizException;
|
|
|
import cn.com.yusys.yusp.commons.exception.ExceptionMessage;
|
|
import cn.com.yusys.yusp.commons.exception.ExceptionMessage;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.BindException;
|
|
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.validation.FieldError;
|
|
|
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.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
|
|
import javax.validation.ConstraintViolationException;
|
|
import javax.validation.ConstraintViolationException;
|
|
@@ -31,5 +34,23 @@ public class GlobalExceptionHandler {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理参数校验异常
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
|
+ public ExceptionMessage handleValidationException(MethodArgumentNotValidException ex) {
|
|
|
|
|
+ ExceptionMessage exceptionMessage = new ExceptionMessage();
|
|
|
|
|
+ BindingResult bindingResult = ex.getBindingResult();
|
|
|
|
|
+ exceptionMessage.setCode("-1");
|
|
|
|
|
+ if (bindingResult.hasErrors() && bindingResult.getFieldError() != null) {
|
|
|
|
|
+ FieldError fieldError = bindingResult.getFieldError();
|
|
|
|
|
+ // 使用校验注解中定义的message
|
|
|
|
|
+ exceptionMessage.setMessage(fieldError.getDefaultMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return exceptionMessage;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|