|
@@ -4,7 +4,6 @@ import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.extra.servlet.JakartaServletUtil;
|
|
|
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
|
|
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
|
@@ -16,10 +15,6 @@ import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
|
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|
|
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
|
|
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
-import jakarta.validation.ConstraintViolation;
|
|
|
-import jakarta.validation.ConstraintViolationException;
|
|
|
-import jakarta.validation.ValidationException;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
@@ -34,18 +29,16 @@ 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;
|
|
|
-import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.validation.ConstraintViolation;
|
|
|
+import javax.validation.ConstraintViolationException;
|
|
|
+import javax.validation.ValidationException;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.METHOD_NOT_ALLOWED;
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
|
|
@@ -97,9 +90,10 @@ public class GlobalExceptionHandler {
|
|
|
if (ex instanceof NoHandlerFoundException) {
|
|
|
return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex);
|
|
|
}
|
|
|
- if (ex instanceof NoResourceFoundException) {
|
|
|
- return noResourceFoundExceptionHandler(request, (NoResourceFoundException) ex);
|
|
|
- }
|
|
|
+ // 仅 JDK17 + Spring Boot 3.X 才有
|
|
|
+// if (ex instanceof NoResourceFoundException) {
|
|
|
+// return noResourceFoundExceptionHandler(request, (NoResourceFoundException) ex);
|
|
|
+// }
|
|
|
if (ex instanceof HttpRequestMethodNotSupportedException) {
|
|
|
return httpRequestMethodNotSupportedExceptionHandler((HttpRequestMethodNotSupportedException) ex);
|
|
|
}
|
|
@@ -205,14 +199,15 @@ public class GlobalExceptionHandler {
|
|
|
return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getRequestURL()));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理 SpringMVC 请求地址不存在
|
|
|
- */
|
|
|
- @ExceptionHandler(NoResourceFoundException.class)
|
|
|
- private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException ex) {
|
|
|
- log.warn("[noResourceFoundExceptionHandler]", ex);
|
|
|
- return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getResourcePath()));
|
|
|
- }
|
|
|
+ // 仅 JDK17 + Spring Boot 3.X 才有
|
|
|
+// /**
|
|
|
+// * 处理 SpringMVC 请求地址不存在
|
|
|
+// */
|
|
|
+// @ExceptionHandler(NoResourceFoundException.class)
|
|
|
+// private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException ex) {
|
|
|
+// log.warn("[noResourceFoundExceptionHandler]", ex);
|
|
|
+// return CommonResult.error(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", ex.getResourcePath()));
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 处理 SpringMVC 请求方法不正确
|
|
@@ -315,12 +310,12 @@ public class GlobalExceptionHandler {
|
|
|
errorLog.setApplicationName(applicationName);
|
|
|
errorLog.setRequestUrl(request.getRequestURI());
|
|
|
Map<String, Object> requestParams = MapUtil.<String, Object>builder()
|
|
|
- .put("query", JakartaServletUtil.getParamMap(request))
|
|
|
- .put("body", JakartaServletUtil.getBody(request)).build();
|
|
|
+ .put("query", ServletUtils.getParamMap(request))
|
|
|
+ .put("body", ServletUtils.getBody(request)).build();
|
|
|
errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
|
|
|
errorLog.setRequestMethod(request.getMethod());
|
|
|
errorLog.setUserAgent(ServletUtils.getUserAgent(request));
|
|
|
- errorLog.setUserIp(JakartaServletUtil.getClientIP(request));
|
|
|
+ errorLog.setUserIp(ServletUtils.getClientIP(request));
|
|
|
errorLog.setExceptionTime(LocalDateTime.now());
|
|
|
}
|
|
|
|