From 4efe025b2e36c56162dc8fcbd05482b7ecc21e5f Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 26 Feb 2025 20:45:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9ENoHandlerFoundExcepti?= =?UTF-8?q?on=E3=80=81HttpRequestMethodNotSupportedException=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=20@dom-w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java index f2f7562d..567b8b58 100644 --- a/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java +++ b/continew-common/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java @@ -17,15 +17,16 @@ package top.continew.admin.common.config.exception; import cn.hutool.core.text.CharSequenceUtil; -import cn.hutool.core.util.NumberUtil; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; +import org.springframework.web.HttpRequestMethodNotSupportedException; 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.multipart.MultipartException; +import org.springframework.web.servlet.NoHandlerFoundException; import top.continew.starter.core.exception.BadRequestException; import top.continew.starter.core.exception.BusinessException; import top.continew.starter.web.model.R; @@ -34,6 +35,7 @@ import top.continew.starter.web.model.R; * 全局异常处理器 * * @author Charles7c + * @author echo * @since 2024/8/7 20:21 */ @Slf4j @@ -73,7 +75,7 @@ public class GlobalExceptionHandler { * 拦截文件上传异常-超过上传大小限制 */ @ExceptionHandler(MultipartException.class) - public R handleRequestTooBigException(MultipartException e, HttpServletRequest request) { + public R handleMultipartException(MultipartException e, HttpServletRequest request) { log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); String msg = e.getMessage(); R defaultFail = R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), msg); @@ -85,14 +87,33 @@ public class GlobalExceptionHandler { if (null != cause) { msg = msg.concat(cause.getMessage().toLowerCase()); } - if (msg.contains("size") && msg.contains("exceed")) { - sizeLimit = CharSequenceUtil.subBetween(msg, "the maximum size ", " for"); - } else if (msg.contains("larger than")) { + if (msg.contains("larger than")) { sizeLimit = CharSequenceUtil.subAfter(msg, "larger than ", true); + } else if (msg.contains("size") && msg.contains("exceed")) { + sizeLimit = CharSequenceUtil.subBetween(msg, "the maximum size ", " for"); } else { return defaultFail; } - String errorMsg = "请上传小于 %sKB 的文件".formatted(NumberUtil.parseLong(sizeLimit) / 1024); - return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg); + return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "请上传小于 %s bytes 的文件".formatted(sizeLimit)); + } + + /** + * 拦截请求 URL 不存在异常 + */ + @ExceptionHandler(NoHandlerFoundException.class) + public R handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request) { + log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); + return R.fail(String.valueOf(HttpStatus.NOT_FOUND.value()), "请求 URL '%s' 不存在".formatted(request + .getRequestURI())); + } + + /** + * 拦截不支持的 HTTP 请求方法异常 + */ + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, + HttpServletRequest request) { + log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e); + return R.fail(String.valueOf(HttpStatus.METHOD_NOT_ALLOWED.value()), "请求方式 '%s' 不支持".formatted(e.getMethod())); } } \ No newline at end of file