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

REST API请求返回404错误?Spring Boot控制器问题排查求助

Troubleshooting 404 Errors in Your Spring Boot REST API

Hey there! Let's break down why your REST API is throwing a 404 error—these are the most common culprits to check one by one:

  • Your controller class is missing the @RestController annotation
    Spring won’t recognize a class as a REST controller unless you mark it with @RestController (or the combination of @Controller + @ResponseBody for older setups). Double-check the top of your controller class—you should see something like this:

    @RestController
    @RequestMapping("/api") // Optional, but adds a consistent base path
    public class YourController {
        // Your mapped endpoints here
    }
    

    Without this annotation, Spring won’t register your endpoint mappings at all.

  • Request path doesn’t match your mapping
    Even if your controller is registered, mismatched paths will trigger a 404. For example:

    • If your method uses @GetMapping("/users"), your request should target http://localhost:8080/users (or http://localhost:8080/api/users if you added a class-level @RequestMapping("/api")).
    • If you’re using @PathVariable, make sure the URL includes the parameter correctly. A method like @GetMapping("/users/{id}") expects a URL like /users/123, not /users?id=123.
  • Spring isn’t scanning your controller package
    Spring Boot automatically scans for components in the same package as your main application class (the one with @SpringBootApplication) and its subpackages. If your controller lives outside this range, Spring won’t find it.
    For example, if your main class is in me.hiboy but your controller is in me.otherpackage, explicitly add a component scan to your main class:

    @SpringBootApplication
    @ComponentScan(basePackages = {"me.hiboy", "me.otherpackage"})
    public class YourApplication {
        public static void main(String[] args) {
            SpringApplication.run(YourApplication.class, args);
        }
    }
    
  • Request method doesn’t match the endpoint’s annotation
    While this usually returns a 405 (Method Not Allowed), some clients might display a 404 instead. Make sure you’re using the right HTTP method:

    • Use GET for @GetMapping endpoints
    • Use POST for @PostMapping endpoints
    • Match PUT/DELETE to their respective annotations too
  • Check your application startup logs
    When Spring Boot starts, it logs all registered endpoint mappings. Look for lines like:

    Mapped GET /users onto public java.util.List<...> me.hiboy.SpringBootRESTAPI.YourController.getUsers()
    If you don’t see your endpoints in these logs, that confirms Spring hasn’t registered your controller—loop back to the first three checks to fix that.

  • Incomplete or miscompiled code
    Your snippet cuts off at org.s..., so double-check that all annotations are properly imported (e.g., org.springframework.web.bind.annotation.PostMapping instead of a wrong package). Also, ensure your project has no compilation errors—check your IDE’s error panel or run mvn clean install (for Maven) to validate.

Start with the first two checks—they’re the most frequent fixes for 404s in Spring Boot REST APIs. If you’re still stuck, sharing your full controller class and main application class would help narrow it down further!

内容的提问来源于stack exchange,提问作者P.K.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:53:32