You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中/error路径REST端点无法被识别的问题

解决Spring自定义/error端点不生效的问题

这问题我之前排查过好多次!核心原因很简单:Spring Boot默认自带了一个处理/error路径的内置控制器BasicErrorController,它会优先接管所有错误请求(比如404、500这类异常场景),所以你自己写的/error端点根本没机会被Spring识别到——这也是为什么改成/foo这类其他路径就能正常工作的原因。

下面给你几个可行的解决方案,按需选择:

方案1:实现ErrorController接口替代默认控制器

这是最直接的方式,自己实现Spring的ErrorController接口,Spring会自动用你的实现替换掉默认的BasicErrorController:

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
public class CustomErrorController implements ErrorController {

    @RequestMapping("/error")
    public ResponseEntity<String> genericErrorHandler(HttpServletRequest request) {
        // 可以从request中获取错误相关的信息,比如状态码
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        
        // 自定义响应状态和内容
        HttpStatus responseStatus = statusCode != null ? HttpStatus.valueOf(statusCode) : HttpStatus.INTERNAL_SERVER_ERROR;
        return ResponseEntity.status(responseStatus).body("自定义REST错误响应");
    }

    // 注意:Spring Boot 2.3及以上版本不需要实现getErrorPath()方法,该方法已被标记为Deprecated
    // 如果你用的是旧版本,需要添加下面这段代码:
    // @Override
    // public String getErrorPath() {
    //     return "/error";
    // }
}

方案2:自定义ErrorAttributes修改错误响应内容

如果只是想调整错误响应的内容,不想替换整个控制器,这种方式更轻量。通过自定义ErrorAttributes bean,你可以修改默认错误响应的字段和内容:

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        // 先获取默认的错误属性集合
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
        
        // 清空默认字段,添加自定义内容
        errorAttributes.clear();
        errorAttributes.put("code", "500");
        errorAttributes.put("msg", "服务器内部错误");
        
        return errorAttributes;
    }
}

这样默认的BasicErrorController会使用你自定义的ErrorAttributes来生成响应,不需要自己写/error端点。

方案3:禁用默认的BasicErrorController(不推荐)

你也可以通过排除自动配置类的方式禁用默认控制器,但这种方式可能会导致其他错误处理逻辑失效,谨慎使用:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;

@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

这种方式会完全禁用Spring Boot的默认错误处理机制,需要你自己处理所有异常场景,所以一般只在特殊需求下使用。

最后验证:启动项目后,访问一个不存在的API路径,或者手动抛出一个异常,应该就能触发你自定义的错误响应了。

内容的提问来源于stack exchange,提问作者Simo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:32:13