Просмотр исходного кода

fix: 后端添加405处理器兼容旧GET请求

liaoxg 3 недель назад
Родитель
Сommit
6a44c9af58

+ 8 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/GlobalExceptionHandler.java

@@ -4,6 +4,7 @@ import com.etotem.cfc.common.Result;
 import javax.servlet.http.HttpServletRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.multipart.MultipartException;
@@ -24,6 +25,13 @@ public class GlobalExceptionHandler {
         return Result.error(400, "请求格式错误,请使用文件表单上传");
     }
 
+    // 旧客户端用 GET 等废弃方法调用已统一为 POST 的接口(如 /api/notices/list),返回干净的 405,不打错误栈
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public Result<Void> handleMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
+        log.warn("请求方法不支持: uri={}, method={}", request.getRequestURI(), e.getMethod());
+        return Result.error(405, "请求方法不支持,本平台接口统一使用POST");
+    }
+
     @ExceptionHandler(Exception.class)
     public Result<Void> handleException(Exception e, HttpServletRequest request) {
         log.error("未捕获异常: {} - {} | uri={}", e.getClass().getName(), e.getMessage(), request.getRequestURI(), e);