访问Tech-O-Dex-API接口返回404错误求助
Hey Billy, sorry to hear you're hitting 404s even though your app starts up and runs on port 8080 (or other ports you've tried). Let's walk through the most likely fixes to get your endpoints working:
1. Verify Your Controller Annotations & Route Mapping
First, double-check your controller class to make sure it's properly annotated and the routes match what you're calling:
- Ensure your controller has
@RestController(or@Controller+@ResponseBodyfor each method) — without this, Spring won't recognize it as a request handler. - Confirm the
@GetMapping,@PostMapping, or@RequestMappingannotation on your method uses the exact path you're testing (/greeting). For example:@RestController public class GreetingController { @PostMapping("/greeting") // Matches POST to /greeting public ResponseEntity<?> handleGreeting(@RequestBody Person person) { // Your logic here return ResponseEntity.ok("Hello " + person.getFirstName() + " " + person.getLastName()); } } - If you have a class-level
@RequestMapping(like@RequestMapping("/api")), your full endpoint path would be/api/greeting— make sure you're not missing that prefix.
2. Check Spring's Component Scanning
Spring Boot automatically scans for components (like controllers) in the same package as your main application class and its sub-packages. If your controller is in a separate package, you'll need to explicitly tell Spring to scan it:
- Add
@ComponentScan(basePackages = {"com.your.main.package", "com.your.controller.package"})to your main application class, replacing the package names with your actual ones. - Alternatively, move your controller class into a sub-package of your main application's package (e.g., if your main class is in
com.techodex, put controllers incom.techodex.controllers).
3. Look for Mapped Endpoints in Startup Logs
When your app starts, Spring prints all registered endpoints to the console. Search for lines like:
Mapped "{[/greeting],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> com.techodex.controller.GreetingController.handleGreeting(com.techodex.model.Person)
If you don't see lines for /greeting or /health, that means Spring isn't picking up your controller at all — go back to checking annotations and component scanning.
4. Confirm Request Method & Headers Match
- If your controller uses
@PostMapping, make sure you're sending a POST request (not GET) when testing/greeting. Using the wrong HTTP method will return a 404. - Double-check that your request includes the correct
Content-Type: application/jsonheader — if you send form data instead of JSON, Spring might not route it to your method.
5. Check for Context Path Configuration
If you've set server.servlet.context-path in your application.properties or application.yml, your endpoints will be prefixed with that path. For example, if you have:
server.servlet.context-path=/techodex
You'll need to access http://localhost:8080/techodex/greeting instead of just /greeting.
6. Rule Out Spring JPA Configuration Issues
Since you're working on the AddingSpringJPA branch, make sure your JPA/datasource configuration isn't silently breaking component registration. Check your startup logs for any WARN or ERROR messages related to JPA — even if the app starts, a misconfigured datasource might prevent some beans (like controllers) from loading properly.
Start with the first two checks (annotations and component scanning) — those are the most common culprits for 404s in Spring Boot apps. Let me know if you find anything off!
内容的提问来源于stack exchange,提问作者Billy

