From 222d4d7a7b8b2a1e93f700e2c24a108384328ba6 Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Thu, 19 Mar 2026 10:51:09 +0800 Subject: [PATCH] =?UTF-8?q?GlobalExceptionHandler=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=85=BC=E5=AE=B9=20=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8NoResourceFoundException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handler/GlobalExceptionHandler.java | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java index 9d0ac3a36..f4a164afc 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java @@ -32,6 +32,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; import jakarta.validation.ValidationException; +import org.springframework.web.servlet.resource.NoResourceFoundException; + import java.time.LocalDateTime; import java.util.Map; import java.util.Objects; @@ -61,38 +63,38 @@ public class GlobalExceptionHandler { * @return 通用返回 */ public CommonResult allExceptionHandler(HttpServletRequest request, Throwable ex) { - if (ex instanceof MissingServletRequestParameterException) { - return missingServletRequestParameterExceptionHandler((MissingServletRequestParameterException) ex); + if (ex instanceof MissingServletRequestParameterException missingServletRequestParameterException) { + return missingServletRequestParameterExceptionHandler(missingServletRequestParameterException); } - if (ex instanceof MethodArgumentTypeMismatchException) { - return methodArgumentTypeMismatchExceptionHandler((MethodArgumentTypeMismatchException) ex); + if (ex instanceof MethodArgumentTypeMismatchException methodArgumentTypeMismatchException) { + return methodArgumentTypeMismatchExceptionHandler(methodArgumentTypeMismatchException); } - if (ex instanceof MethodArgumentNotValidException) { - return methodArgumentNotValidExceptionExceptionHandler((MethodArgumentNotValidException) ex); + if (ex instanceof MethodArgumentNotValidException methodArgumentNotValidException) { + return methodArgumentNotValidExceptionExceptionHandler(methodArgumentNotValidException); } - if (ex instanceof BindException) { - return bindExceptionHandler((BindException) ex); + if (ex instanceof BindException bindException) { + return bindExceptionHandler(bindException); } - if (ex instanceof ConstraintViolationException) { - return constraintViolationExceptionHandler((ConstraintViolationException) ex); + if (ex instanceof ConstraintViolationException constraintViolationException) { + return constraintViolationExceptionHandler(constraintViolationException); } - if (ex instanceof ValidationException) { - return validationException((ValidationException) ex); + if (ex instanceof ValidationException validationException) { + return validationException(validationException); } - if (ex instanceof NoHandlerFoundException) { - return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex); + if (ex instanceof NoHandlerFoundException noHandlerFoundException) { + return noHandlerFoundExceptionHandler(noHandlerFoundException); } - if (ex instanceof HttpRequestMethodNotSupportedException) { - return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); + if (ex instanceof HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) { + return httpRequestMethodNotSupportedExceptionHandler(httpRequestMethodNotSupportedException); } - if (ex instanceof ServiceException) { - return serviceExceptionHandler((ServiceException) ex); + if (ex instanceof ServiceException serviceException) { + return serviceExceptionHandler(serviceException); } - if (ex instanceof AccessDeniedException) { - return accessDeniedExceptionHandler(request, (AccessDeniedException) ex); + if (ex instanceof AccessDeniedException accessDeniedException) { + return accessDeniedExceptionHandler(request, accessDeniedException); } - if (ex instanceof HttpMessageNotReadableException) { - return httpMessageNotReadableExceptionHandler(request, (HttpMessageNotReadableException) ex); + if (ex instanceof HttpMessageNotReadableException httpMessageNotReadableException) { + return httpMessageNotReadableExceptionHandler(request, httpMessageNotReadableException); } return defaultExceptionHandler(request, ex); } @@ -250,6 +252,18 @@ public class GlobalExceptionHandler { return CommonResult.error(DUPLICATE_DATA_ERROR); } + /** + * 处理资源不存在异常 NoResourceFoundException + * + * @param ex + * @return + */ + @ExceptionHandler(value = NoResourceFoundException.class) + public CommonResult noResourceFoundExceptionHandler(NoResourceFoundException ex) { + log.warn(String.format("==========[noResourceFoundExceptionHandler]==========,%s", ex.getMessage())); + return CommonResult.error(NOT_FOUND, ex.getResourcePath()); + } + /** * 处理系统异常,兜底处理所有的一切 */