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

Spring Boot接口调用返回404 Not Found问题求助

排查Spring Boot接口404问题

核心原因

控制器类中@RequestMapping注解的参数使用错误:用name属性指定路径,而非value/path属性。

@RequestMapping的name属性仅用于给请求映射设置一个描述性名称,并不参与请求路径的匹配逻辑。真正负责绑定请求路径的是value(或path)属性,因此当前的控制器方法并未绑定到/customer路径,导致请求无法匹配,返回404。

修正方案

将控制器中的@RequestMapping注解的name参数替换为value(或path),或者使用更简洁的@GetMapping/@PostMapping注解:

修正后的CustomerController代码

@RestController
public class CustomerController {
    
    // 方式1:使用value属性指定路径
    @RequestMapping(value="/customer", method=RequestMethod.GET)
    public Customer getCustomer(@RequestParam Long id) {
        Customer cust = new Customer();
        cust.setId(id);
        cust.setName("George");
        return cust;
    }
    
    // 方式2:使用PostMapping简化写法
    @PostMapping(value="/customer", consumes = "application/json")
    public Customer createCustomer(@RequestBody Customer cust) {
        return cust;
    }
}

额外验证建议

如果想确认请求映射是否正确注册,可以在application.properties中添加配置开启Spring MVC的调试日志:

logging.level.org.springframework.web.servlet.mvc.method.annotation=DEBUG

启动应用后,会在日志中看到类似如下的请求映射注册信息,确认/customer路径已被正确绑定:

RequestMappingHandlerMapping : Mapped "{[/customer],methods=[GET]}" onto public com.example.Customer com.example.CustomerController.getCustomer(java.lang.Long)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 05:05:32