From 754ef0639baf097fb67f59bedc7c1a7e11714c5b Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 25 Mar 2025 20:04:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20MissingServletRequ?= =?UTF-8?q?estParameterException=E3=80=81HttpMessageNotReadableException?= =?UTF-8?q?=20=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) 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 5b34dddd..1222e15b 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 @@ -18,11 +18,14 @@ package top.continew.admin.common.config.exception; import cn.hutool.core.io.FileUtil; import cn.hutool.core.text.CharSequenceUtil; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; 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.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; @@ -45,7 +48,7 @@ import top.continew.starter.web.model.R; public class GlobalExceptionHandler { /** - * 拦截业务异常 + * 业务异常 */ @ExceptionHandler(BusinessException.class) public R handleBusinessException(BusinessException e, HttpServletRequest request) { @@ -54,7 +57,10 @@ public class GlobalExceptionHandler { } /** - * 拦截自定义验证异常-错误请求 + * 自定义验证异常-错误请求 + *

+ * {@code ValidationUtils.throwIfXxx(xxx)} + *

*/ @ExceptionHandler(BadRequestException.class) public R handleBadRequestException(BadRequestException e, HttpServletRequest request) { @@ -63,7 +69,23 @@ public class GlobalExceptionHandler { } /** - * 拦截校验异常-方法参数类型不匹配异常 + * 方法参数缺失异常 + *

+ * {@code @RequestParam} 参数缺失 + *

+ */ + @ExceptionHandler(MissingServletRequestParameterException.class) + public R handleMethodArgumentTypeMismatchException(MissingServletRequestParameterException e, + HttpServletRequest request) { + log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); + return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "参数 '%s' 缺失".formatted(e.getParameterName())); + } + + /** + * 方法参数类型不匹配异常 + *

+ * {@code @RequestParam} 参数类型不匹配 + *

*/ @ExceptionHandler(MethodArgumentTypeMismatchException.class) public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, @@ -73,7 +95,27 @@ public class GlobalExceptionHandler { } /** - * 拦截文件上传异常-超过上传大小限制 + * HTTP 消息不可读异常 + *

+ * 1.@RequestBody 缺失请求体
+ * 2.@RequestBody 实体内参数类型不匹配
+ * 3.请求体解析格式异常
+ * ... + *

+ */ + @ExceptionHandler(HttpMessageNotReadableException.class) + public R handleHttpMessageNotReadableException(HttpMessageNotReadableException e, HttpServletRequest request) { + log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); + // @RequestBody 实体内参数类型不匹配 + if (e.getCause() instanceof InvalidFormatException invalidFormatException) { + return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "参数 '%s' 类型不匹配" + .formatted(invalidFormatException.getValue())); + } + return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "参数缺失或格式不正确"); + } + + /** + * 文件上传异常-超过上传大小限制 */ @ExceptionHandler(MultipartException.class) public R handleMultipartException(MultipartException e, HttpServletRequest request) { @@ -100,7 +142,7 @@ public class GlobalExceptionHandler { } /** - * 拦截请求 URL 不存在异常 + * 请求 URL 不存在异常 */ @ExceptionHandler(NoHandlerFoundException.class) public R handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request) { @@ -110,7 +152,7 @@ public class GlobalExceptionHandler { } /** - * 拦截不支持的 HTTP 请求方法异常 + * 不支持的 HTTP 请求方法异常 */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e,