From d1450061d0154c7eec81531cbcede0676959e579 Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Sat, 22 Mar 2025 10:36:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/core/handler/GlobalExceptionHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java index 51ec890db..8436acb14 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java @@ -14,6 +14,7 @@ import com.cf.imes.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.security.access.AccessDeniedException; @@ -144,7 +145,7 @@ public class GlobalExceptionHandler { String defaultMessage = fieldError.getDefaultMessage(); // 如果是校验器来的给出校验注解的提示 if (CharSequenceUtil.isNotEmpty(defaultMessage) && fieldError.contains(ConstraintViolation.class)) { - return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", defaultMessage)); + return CommonResult.error(BAD_REQUEST.getCode(), defaultMessage); } else { return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确,%s:%s", fieldError.getField(), fieldError.getRejectedValue())); } @@ -157,7 +158,12 @@ public class GlobalExceptionHandler { public CommonResult constraintViolationExceptionHandler(ConstraintViolationException ex) { log.warn("[constraintViolationExceptionHandler]", ex); ConstraintViolation constraintViolation = ex.getConstraintViolations().iterator().next(); - return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", constraintViolation.getMessage())); + String message = constraintViolation.getMessage(); + if (StringUtils.isNotEmpty(message)) { + return CommonResult.error(BAD_REQUEST.getCode(), message); + } else { + return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", message)); + } } /**