Spring Boot控制器所有API端点返回404问题求助
Hey there! Let's dig into that 404 issue you're hitting with your Spring API. I see your controller code got cut off halfway through the @Autowired line, so I'll start by filling in what's likely the rest of it based on standard Spring practices, then walk through the most common fixes for this problem.
First, here's what your complete controller might look like (adjust if your actual endpoints differ):
package com.example.controllers; import com.example.models.Movie; import com.example.services.MovieService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; @RestController public class MovieController { @Autowired private MovieService movieService; // Example GET endpoint @GetMapping("/movies") public List<Movie> getAllMovies() { return movieService.getAllMovies(); } // Example GET by ID endpoint @GetMapping("/movies/{id}") public Optional<Movie> getMovieById(@PathVariable Long id) { return movieService.getMovieById(id); } }
Now let's go through the most likely reasons your endpoints are returning 404:
Check package scanning for your controller
Spring needs to detect yourMovieControllerto register its endpoints. Make sure your main application class (the one annotated with@SpringBootApplication) is in a parent package of your controller. For example, if your app class lives incom.example, your controller incom.example.controllerswill be scanned automatically. If your controller is in a separate package, add@ComponentScan(basePackages = "com.example.controllers")to your main application class to tell Spring where to look.Verify your request mapping paths
Double-check the paths in your@GetMapping,@PostMapping, etc. If you added a class-level@RequestMapping("/api")to your controller, your full endpoint would be/api/moviesinstead of just/movies. Also, watch out for typos in the URL you're testing (while Spring's path matching is case-insensitive by default, it's easy to mistype a segment like/movieinstead of/movies).Confirm you're using the correct port
By default, Spring Boot runs on port 8080. If you changed the port inapplication.properties(withserver.port=xxxx) orapplication.yml, make sure you're sending requests to that port (e.g.,http://localhost:8081/moviesinstead ofhttp://localhost:8080/movies).Check for a server context path
If you configured a context path (likeserver.servlet.context-path=/myappin your properties file), all your endpoints will be prefixed with that path. So your/moviesendpoint becomeshttp://localhost:8080/myapp/movies.Ensure your service is properly annotated
Make sure yourMovieServiceclass is annotated with@Service—this tells Spring to create a bean for it, which is required for@Autowiredto work. While a missing@Serviceusually throws a startup error, it's worth ruling out if your app is starting without errors but still returning 404s.Use Spring Boot Actuator to inspect mappings (optional but powerful)
If you add the Actuator dependency to your project (includespring-boot-starter-actuatorin your pom.xml or build.gradle), you can hit the/actuator/mappingsendpoint to see every registered path in your app. This will show you exactly what Spring has mapped, which is a great way to confirm if your controller's endpoints are being picked up at all.
If you can share the rest of your controller code and your main application class, I can help narrow down the issue even further!
内容的提问来源于stack exchange,提问作者relentless-coder

