Commit 176e87141eec2ff13ff089eb10bec9f15102a817

Authored by YunaiV
1 parent ab7d9fdf

feat:GlobalExceptionHandler 支持更多请求参数不对的问题

yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
... ... @@ -6,6 +6,8 @@ import cn.hutool.core.map.MapUtil;
6 6 import cn.hutool.core.util.ObjUtil;
7 7 import cn.hutool.core.util.StrUtil;
8 8 import cn.hutool.extra.servlet.JakartaServletUtil;
  9 +import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
  10 +import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO;
9 11 import cn.iocoder.yudao.framework.common.exception.ServiceException;
10 12 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
11 13 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
... ... @@ -14,8 +16,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
14 16 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
15 17 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
16 18 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
17   -import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
18   -import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO;
19 19 import com.fasterxml.jackson.databind.exc.InvalidFormatException;
20 20 import jakarta.servlet.http.HttpServletRequest;
21 21 import jakarta.validation.ConstraintViolation;
... ... @@ -29,6 +29,7 @@ import org.springframework.util.Assert;
29 29 import org.springframework.validation.BindException;
30 30 import org.springframework.validation.FieldError;
31 31 import org.springframework.validation.ObjectError;
  32 +import org.springframework.web.HttpMediaTypeNotSupportedException;
32 33 import org.springframework.web.HttpRequestMethodNotSupportedException;
33 34 import org.springframework.web.bind.MethodArgumentNotValidException;
34 35 import org.springframework.web.bind.MissingServletRequestParameterException;
... ... @@ -101,6 +102,9 @@ public class GlobalExceptionHandler {
101 102 if (ex instanceof HttpRequestMethodNotSupportedException) {
102 103 return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
103 104 }
  105 + if (ex instanceof HttpMediaTypeNotSupportedException) {
  106 + return httpMediaTypeNotSupportedExceptionHandler((HttpMediaTypeNotSupportedException) ex);
  107 + }
104 108 if (ex instanceof ServiceException) {
105 109 return serviceExceptionHandler((ServiceException) ex);
106 110 }
... ... @@ -179,9 +183,11 @@ public class GlobalExceptionHandler {
179 183 if (ex.getCause() instanceof InvalidFormatException) {
180 184 InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause();
181 185 return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue()));
182   - } else {
183   - return defaultExceptionHandler(ServletUtils.getRequest(), ex);
184 186 }
  187 + if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) {
  188 + return CommonResult.error(BAD_REQUEST.getCode(), "请求参数类型错误: request body 缺失");
  189 + }
  190 + return defaultExceptionHandler(ServletUtils.getRequest(), ex);
185 191 }
186 192  
187 193 /**
... ... @@ -238,6 +244,17 @@ public class GlobalExceptionHandler {
238 244 }
239 245  
240 246 /**
  247 + * 处理 SpringMVC 请求的 Content-Type 不正确
  248 + *
  249 + * 例如说,A 接口的 Content-Type 为 application/json,结果请求的 Content-Type 为 application/octet-stream,导致不匹配
  250 + */
  251 + @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
  252 + public CommonResult<?> httpMediaTypeNotSupportedExceptionHandler(HttpMediaTypeNotSupportedException ex) {
  253 + log.warn("[httpMediaTypeNotSupportedExceptionHandler]", ex);
  254 + return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求类型不正确:%s", ex.getMessage()));
  255 + }
  256 +
  257 + /**
241 258 * 处理 Spring Security 权限不足的异常
242 259 *
243 260 * 来源是,使用 @PreAuthorize 注解,AOP 进行权限拦截
... ...