Commit 047051e0087218bafe3169c71ea328ad1b2b36c1

Authored by YunaiV
1 parent bbdf9e9b

【功能优化】全局:处理 HttpRequestMethodNotSupportedException 异常

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,6 +30,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
30 import org.springframework.web.bind.annotation.RestControllerAdvice; 30 import org.springframework.web.bind.annotation.RestControllerAdvice;
31 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; 31 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
32 import org.springframework.web.servlet.NoHandlerFoundException; 32 import org.springframework.web.servlet.NoHandlerFoundException;
  33 +import org.springframework.web.servlet.resource.NoResourceFoundException;
33 34
34 import java.time.LocalDateTime; 35 import java.time.LocalDateTime;
35 import java.util.Map; 36 import java.util.Map;
@@ -87,6 +88,9 @@ public class GlobalExceptionHandler { @@ -87,6 +88,9 @@ public class GlobalExceptionHandler {
87 if (ex instanceof NoHandlerFoundException) { 88 if (ex instanceof NoHandlerFoundException) {
88 return noHandlerFoundExceptionHandler(request, (NoHandlerFoundException) ex); 89 return noHandlerFoundExceptionHandler(request, (NoHandlerFoundException) ex);
89 } 90 }
  91 + if (ex instanceof NoResourceFoundException) {
  92 + return noResourceFoundExceptionHandler(request, (NoResourceFoundException) ex);
  93 + }
90 if (ex instanceof HttpRequestMethodNotSupportedException) { 94 if (ex instanceof HttpRequestMethodNotSupportedException) {
91 return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); 95 return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
92 } 96 }
@@ -177,6 +181,15 @@ public class GlobalExceptionHandler { @@ -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 * 处理 SpringMVC 请求方法不正确 193 * 处理 SpringMVC 请求方法不正确
181 * 194 *
182 * 例如说,A 接口的方法为 GET 方式,结果请求方法为 POST 方式,导致不匹配 195 * 例如说,A 接口的方法为 GET 方式,结果请求方法为 POST 方式,导致不匹配