REST API请求返回404错误?Spring Boot控制器问题排查求助
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
@RestControllerannotation
Spring won’t recognize a class as a REST controller unless you mark it with@RestController(or the combination of@Controller+@ResponseBodyfor 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 targethttp://localhost:8080/users(orhttp://localhost:8080/api/usersif 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.
- If your method uses
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 inme.hiboybut your controller is inme.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
GETfor@GetMappingendpoints - Use
POSTfor@PostMappingendpoints - Match
PUT/DELETEto their respective annotations too
- Use
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 atorg.s..., so double-check that all annotations are properly imported (e.g.,org.springframework.web.bind.annotation.PostMappinginstead of a wrong package). Also, ensure your project has no compilation errors—check your IDE’s error panel or runmvn 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.

