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.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());
}
/**
* 处理系统异常,兜底处理所有的一切
*/