From 0726a21eadd2ce2862a1792aa3acd4ce4d1c1213 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 17 May 2025 14:33:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20GlobalExceptionHandler=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20MethodArgumentNotValidException=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=88=E4=B9=8B=E5=89=8D=E4=BD=BF=E7=94=A8=20Gracef?= =?UTF-8?q?ul=20Reponse=20=E6=89=98=E7=AE=A1=E4=BC=9A=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=85=A8=E9=83=A8=E8=BF=94=E5=9B=9E=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java index b7862c6f..e1bfbcd7 100644 --- a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java +++ b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java @@ -25,6 +25,7 @@ import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -34,8 +35,11 @@ import org.springframework.web.servlet.NoHandlerFoundException; import top.continew.starter.core.exception.BadRequestException; import top.continew.starter.core.exception.BaseException; import top.continew.starter.core.exception.BusinessException; +import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.web.model.R; +import java.util.Objects; + /** * 全局异常处理器 * @@ -48,7 +52,6 @@ import top.continew.starter.web.model.R; @RestControllerAdvice public class GlobalExceptionHandler { - /** * 自定义基类异常 */ @@ -92,6 +95,20 @@ public class GlobalExceptionHandler { return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "参数 '%s' 缺失".formatted(e.getParameterName())); } + /** + * 方法参数无效异常 + *

+ * {@code @NotBlank}、{@code @NotNull} 等参数验证不通过 + *

+ */ + @ExceptionHandler(MethodArgumentNotValidException.class) + public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) { + log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); + String errorMsg = ExceptionUtils.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()) + .getDefaultMessage()); + return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg); + } + /** * 方法参数类型不匹配异常 *