Spring Boot API调用返回404错误原因咨询
Hey there, let's break down why your test API is returning a 404 after deploying your Spring Boot JAR to the embedded Tomcat. Based on the code snippet you shared, here are the most likely culprits:
1. Typo in Component Scan Path
Looking at your @ComponentScan annotation, you've got a partial package path: com.telematics.flee... — that's a missing "t" (should be com.telematics.fleet). This means Spring isn't scanning your controller package correctly, so it can't find and register your API endpoints. That's a classic reason for 404s!
Fix this by correcting the package path in your @ComponentScan (or even better, since @SpringBootApplication already includes component scanning for the main class's package and subpackages, you might not even need the explicit @ComponentScan if your controller is under com.telematics.fleet.controller). Here's the corrected main class code:
package com.telematics.fleet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages={"com.telematics.fleet.controller", "com.telematics.fleet"}) // Fixed the typo here public class FleetApplication { public static void main(String[] args) { SpringApplication.run(FleetApplication.class, args); } }
2. Missing or Incorrect Controller Annotations
Double-check your controller class to make sure it's annotated properly:
- Use
@RestController(combines@Controllerand@ResponseBody) if you're building REST APIs. - Ensure your endpoint methods have the right
@RequestMapping(or@GetMapping,@PostMapping, etc.) with the correct path. For example:@RestController @RequestMapping("/api/test") public class TestController { @GetMapping public String testEndpoint() { return "Hello from test API!"; } }
3. Mismatched Request Path or HTTP Method
When testing the API, make sure you're using the exact path defined in your controller, including any leading slashes, and that the HTTP method (GET/POST/PUT/etc.) matches what you've annotated. For example, if your endpoint uses @PostMapping, a GET request will return 404.
4. Main Class Location
If you remove the explicit @ComponentScan, Spring Boot will automatically scan all packages under your main class's package (com.telematics.fleet). As long as your controller is in com.telematics.fleet.controller (a subpackage), this should work without extra configuration. So you could even simplify your main class to just:
package com.telematics.fleet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FleetApplication { public static void main(String[] args) { SpringApplication.run(FleetApplication.class, args); } }
Give these fixes a try, and your 404 error should disappear once Spring can properly locate and register your API endpoints.
内容的提问来源于stack exchange,提问作者Vinayaka M Malya

