From 46edab4e3aac59509ee375c38ed9d38968ce20cc 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:46:01 +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 | 14 +++++++++++++- 1 file changed, 13 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 8fb1f761..a956a17a 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 @@ -22,6 +22,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; /** * 全局异常处理器 @@ -80,7 +81,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()); } /** @@ -122,6 +123,17 @@ 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()); + } + + /** * 拦截未知的运行时异常 */