From e142d5f4d83a2ec92a21f8b532f2f0392df4784d Mon Sep 17 00:00:00 2001 From: huyupeng Date: Tue, 11 Oct 2022 17:25:47 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E9=AA=8C=E8=AF=81=E5=BC=82=E5=B8=B8=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/GlobalExceptionHandler.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java index 998b78f5..96593c9a 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java @@ -1,9 +1,14 @@ package com.ruoyi.common.security.handler; import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -17,6 +22,8 @@ import com.ruoyi.common.core.exception.auth.NotRoleException; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; +import java.util.List; + /** * 全局异常处理器 * @@ -111,9 +118,19 @@ public class GlobalExceptionHandler @ExceptionHandler(MethodArgumentNotValidException.class) public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { - log.error(e.getMessage(), e); - String message = e.getBindingResult().getFieldError().getDefaultMessage(); - return AjaxResult.error(message); + BindingResult result = e.getBindingResult(); + List errors = result.getAllErrors(); + String message = "请填写正确信息"; + if (!result.hasErrors() || CollectionUtils.isEmpty(errors)) { + return AjaxResult.error(message); + } + errors.forEach(p -> { + FieldError fieldError = (FieldError) p; + log.warn("Data check failure: object[{}],field[{}],errorMessage[{}]", fieldError.getObjectName(), + fieldError.getField(), fieldError.getDefaultMessage()); + }); + FieldError fieldError = (FieldError) errors.get(0); + return AjaxResult.error(StringUtils.isEmpty(fieldError.getDefaultMessage()) ? message : fieldError.getDefaultMessage()); } /**