Java Spring(Gradle) RestAPI编译后Postman请求返回404求助
Hey there, I totally get the frustration of hitting a 404 when you’ve just finished writing your controller code. Let’s break down the most likely reasons this is happening and how to fix them, using the code snippet you provided as a starting point.
First, let’s properly format your controller code for clarity:
package arma.Arma.RestController; import arma.Arma.Service.UserService; import arma.Arma.doman.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping(value = "api/user") public class UserController { // Assuming this is your controller class name @Autowired private UserService userService; // Missing method-level request mappings here? }
Now let’s go through the key checks:
Verify Package Scanning
Spring Boot automatically scans components (like@RestController) in the same package as your main application class (the one with@SpringBootApplication) and its sub-packages. If your main class is inarma.Arma, your controller inarma.Arma.RestControlleris fine. But if your main class is in a different root package (e.g.,arma), you’ll need to explicitly add@ComponentScan(basePackages = "arma.Arma")to your main class to tell Spring where to look for controllers.Check Request Mapping Paths
Your class uses@RequestMapping(value = "api/user")—while Spring accepts paths without a leading slash, it’s safer to use/api/userto avoid unexpected behavior with context paths. When testing with Postman, make sure your request URL matches:- Default port/context:
http://localhost:8080/api/user - If you set a context path in
application.properties(e.g.,server.servlet.context-path=/myapp), usehttp://localhost:8080/myapp/api/user
- Default port/context:
Don’t Forget Method-Level Annotations
The biggest culprit here is often missing method mappings! Your controller class is annotated, but if you don’t add@GetMapping,@PostMapping, etc., to your methods, Spring won’t know which endpoints to expose. For example:@GetMapping public List<User> getAllUsers() { return userService.getAllUsers(); }Without this, hitting
/api/userwill return 404 because there’s no method handling that request.Check Application Startup Logs
When you start your Spring Boot app, look for log lines likeMapped "{[/api/user]}" onto public java.util.List<arma.Arma.doman.User> arma.Arma.RestController.UserController.getAllUsers(). If you don’t see lines like this, Spring isn’t recognizing your controller—this points back to package scanning or missing annotations.Confirm Gradle Dependencies
Make sure yourbuild.gradleincludes the Spring Web starter, which is required for REST functionality:dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // Other dependencies... }Without this, Spring won’t start an embedded web server (like Tomcat), so no HTTP requests can be handled at all.
If you’ve checked all these and still get a 404, feel free to share more details: your controller methods, application properties, or startup logs, and we can dig deeper!
内容的提问来源于stack exchange,提问作者Viktor Kenéz

