From 2782c369c9317c916b4b63ea9589c8588ce7ed7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 30 Apr 2024 16:25:51 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=B8=B8?= =?UTF-8?q?=E8=A7=84web=E5=BC=82=E5=B8=B8=E7=8A=B6=E6=80=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/handler/GlobalExceptionHandler.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java index a4dcfe3fa..f2e573f9e 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java @@ -21,6 +21,7 @@ import org.springframework.web.bind.MissingPathVariableException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.servlet.NoHandlerFoundException; /** * 全局异常处理器 @@ -69,7 +70,7 @@ public class GlobalExceptionHandler { HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); - return R.fail(e.getMessage()); + return R.fail(HttpStatus.HTTP_BAD_METHOD, e.getMessage()); } /** @@ -111,6 +112,16 @@ public class GlobalExceptionHandler { return R.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); } + /** + * 找不到路由 + */ + @ExceptionHandler(NoHandlerFoundException.class) + public R handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}'不存在.", requestURI); + return R.fail(HttpStatus.HTTP_NOT_FOUND, e.getMessage()); + } + /** * 拦截未知的运行时异常 */