Commit 176e87141eec2ff13ff089eb10bec9f15102a817
1 parent
ab7d9fdf
feat:GlobalExceptionHandler 支持更多请求参数不对的问题
Showing
1 changed file
with
21 additions
and
4 deletions
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 +6,8 @@ import cn.hutool.core.map.MapUtil; | ||
| 6 | import cn.hutool.core.util.ObjUtil; | 6 | import cn.hutool.core.util.ObjUtil; |
| 7 | import cn.hutool.core.util.StrUtil; | 7 | import cn.hutool.core.util.StrUtil; |
| 8 | import cn.hutool.extra.servlet.JakartaServletUtil; | 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 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 11 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 10 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | 12 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 11 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 13 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| @@ -14,8 +16,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | @@ -14,8 +16,6 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 14 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | 16 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
| 15 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 17 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 16 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 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 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; | 19 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 20 | import jakarta.servlet.http.HttpServletRequest; | 20 | import jakarta.servlet.http.HttpServletRequest; |
| 21 | import jakarta.validation.ConstraintViolation; | 21 | import jakarta.validation.ConstraintViolation; |
| @@ -29,6 +29,7 @@ import org.springframework.util.Assert; | @@ -29,6 +29,7 @@ import org.springframework.util.Assert; | ||
| 29 | import org.springframework.validation.BindException; | 29 | import org.springframework.validation.BindException; |
| 30 | import org.springframework.validation.FieldError; | 30 | import org.springframework.validation.FieldError; |
| 31 | import org.springframework.validation.ObjectError; | 31 | import org.springframework.validation.ObjectError; |
| 32 | +import org.springframework.web.HttpMediaTypeNotSupportedException; | ||
| 32 | import org.springframework.web.HttpRequestMethodNotSupportedException; | 33 | import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 33 | import org.springframework.web.bind.MethodArgumentNotValidException; | 34 | import org.springframework.web.bind.MethodArgumentNotValidException; |
| 34 | import org.springframework.web.bind.MissingServletRequestParameterException; | 35 | import org.springframework.web.bind.MissingServletRequestParameterException; |
| @@ -101,6 +102,9 @@ public class GlobalExceptionHandler { | @@ -101,6 +102,9 @@ public class GlobalExceptionHandler { | ||
| 101 | if (ex instanceof HttpRequestMethodNotSupportedException) { | 102 | if (ex instanceof HttpRequestMethodNotSupportedException) { |
| 102 | return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); | 103 | return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); |
| 103 | } | 104 | } |
| 105 | + if (ex instanceof HttpMediaTypeNotSupportedException) { | ||
| 106 | + return httpMediaTypeNotSupportedExceptionHandler((HttpMediaTypeNotSupportedException) ex); | ||
| 107 | + } | ||
| 104 | if (ex instanceof ServiceException) { | 108 | if (ex instanceof ServiceException) { |
| 105 | return serviceExceptionHandler((ServiceException) ex); | 109 | return serviceExceptionHandler((ServiceException) ex); |
| 106 | } | 110 | } |
| @@ -179,9 +183,11 @@ public class GlobalExceptionHandler { | @@ -179,9 +183,11 @@ public class GlobalExceptionHandler { | ||
| 179 | if (ex.getCause() instanceof InvalidFormatException) { | 183 | if (ex.getCause() instanceof InvalidFormatException) { |
| 180 | InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause(); | 184 | InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause(); |
| 181 | return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue())); | 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,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 | * 处理 Spring Security 权限不足的异常 | 258 | * 处理 Spring Security 权限不足的异常 |
| 242 | * | 259 | * |
| 243 | * 来源是,使用 @PreAuthorize 注解,AOP 进行权限拦截 | 260 | * 来源是,使用 @PreAuthorize 注解,AOP 进行权限拦截 |