Commit 047051e0087218bafe3169c71ea328ad1b2b36c1
1 parent
bbdf9e9b
【功能优化】全局:处理 HttpRequestMethodNotSupportedException 异常
Showing
1 changed file
with
13 additions
and
0 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
| ... | ... | @@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; |
| 30 | 30 | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 31 | 31 | import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| 32 | 32 | import org.springframework.web.servlet.NoHandlerFoundException; |
| 33 | +import org.springframework.web.servlet.resource.NoResourceFoundException; | |
| 33 | 34 | |
| 34 | 35 | import java.time.LocalDateTime; |
| 35 | 36 | import java.util.Map; |
| ... | ... | @@ -87,6 +88,9 @@ public class GlobalExceptionHandler { |
| 87 | 88 | if (ex instanceof NoHandlerFoundException) { |
| 88 | 89 | return noHandlerFoundExceptionHandler(request, (NoHandlerFoundException) ex); |
| 89 | 90 | } |
| 91 | + if (ex instanceof NoResourceFoundException) { | |
| 92 | + return noResourceFoundExceptionHandler(request, (NoResourceFoundException) ex); | |
| 93 | + } | |
| 90 | 94 | if (ex instanceof HttpRequestMethodNotSupportedException) { |
| 91 | 95 | return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); |
| 92 | 96 | } |
| ... | ... | @@ -177,6 +181,15 @@ public class GlobalExceptionHandler { |
| 177 | 181 | } |
| 178 | 182 | |
| 179 | 183 | /** |
| 184 | + * 处理 SpringMVC 请求地址不存在 | |
| 185 | + */ | |
| 186 | + @ExceptionHandler(NoResourceFoundException.class) | |
| 187 | + private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException ex) { | |
| 188 | + log.warn("[noResourceFoundExceptionHandler]", ex); | |
| 189 | + return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getResourcePath())); | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 180 | 193 | * 处理 SpringMVC 请求方法不正确 |
| 181 | 194 | * |
| 182 | 195 | * 例如说,A 接口的方法为 GET 方式,结果请求方法为 POST 方式,导致不匹配 | ... | ... |