GlobalExceptionHandler全局异常兼容 资源不存在NoResourceFoundException

This commit is contained in:
gaoqr
2026-03-19 10:51:09 +08:00
parent b8f0c14945
commit 222d4d7a7b
@@ -32,6 +32,8 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException; import jakarta.validation.ValidationException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@@ -61,38 +63,38 @@ public class GlobalExceptionHandler {
* @return 通用返回 * @return 通用返回
*/ */
public CommonResult allExceptionHandler(HttpServletRequest request, Throwable ex) { public CommonResult allExceptionHandler(HttpServletRequest request, Throwable ex) {
if (ex instanceof MissingServletRequestParameterException) { if (ex instanceof MissingServletRequestParameterException missingServletRequestParameterException) {
return missingServletRequestParameterExceptionHandler((MissingServletRequestParameterException) ex); return missingServletRequestParameterExceptionHandler(missingServletRequestParameterException);
} }
if (ex instanceof MethodArgumentTypeMismatchException) { if (ex instanceof MethodArgumentTypeMismatchException methodArgumentTypeMismatchException) {
return methodArgumentTypeMismatchExceptionHandler((MethodArgumentTypeMismatchException) ex); return methodArgumentTypeMismatchExceptionHandler(methodArgumentTypeMismatchException);
} }
if (ex instanceof MethodArgumentNotValidException) { if (ex instanceof MethodArgumentNotValidException methodArgumentNotValidException) {
return methodArgumentNotValidExceptionExceptionHandler((MethodArgumentNotValidException) ex); return methodArgumentNotValidExceptionExceptionHandler(methodArgumentNotValidException);
} }
if (ex instanceof BindException) { if (ex instanceof BindException bindException) {
return bindExceptionHandler((BindException) ex); return bindExceptionHandler(bindException);
} }
if (ex instanceof ConstraintViolationException) { if (ex instanceof ConstraintViolationException constraintViolationException) {
return constraintViolationExceptionHandler((ConstraintViolationException) ex); return constraintViolationExceptionHandler(constraintViolationException);
} }
if (ex instanceof ValidationException) { if (ex instanceof ValidationException validationException) {
return validationException((ValidationException) ex); return validationException(validationException);
} }
if (ex instanceof NoHandlerFoundException) { if (ex instanceof NoHandlerFoundException noHandlerFoundException) {
return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex); return noHandlerFoundExceptionHandler(noHandlerFoundException);
} }
if (ex instanceof HttpRequestMethodNotSupportedException) { if (ex instanceof HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) {
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex); return httpRequestMethodNotSupportedExceptionHandler(httpRequestMethodNotSupportedException);
} }
if (ex instanceof ServiceException) { if (ex instanceof ServiceException serviceException) {
return serviceExceptionHandler((ServiceException) ex); return serviceExceptionHandler(serviceException);
} }
if (ex instanceof AccessDeniedException) { if (ex instanceof AccessDeniedException accessDeniedException) {
return accessDeniedExceptionHandler(request, (AccessDeniedException) ex); return accessDeniedExceptionHandler(request, accessDeniedException);
} }
if (ex instanceof HttpMessageNotReadableException) { if (ex instanceof HttpMessageNotReadableException httpMessageNotReadableException) {
return httpMessageNotReadableExceptionHandler(request, (HttpMessageNotReadableException) ex); return httpMessageNotReadableExceptionHandler(request, httpMessageNotReadableException);
} }
return defaultExceptionHandler(request, ex); return defaultExceptionHandler(request, ex);
} }
@@ -250,6 +252,18 @@ public class GlobalExceptionHandler {
return CommonResult.error(DUPLICATE_DATA_ERROR); 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());
}
/** /**
* 处理系统异常,兜底处理所有的一切 * 处理系统异常,兜底处理所有的一切
*/ */