You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Spring(Gradle) RestAPI编译后Postman请求返回404求助

Troubleshooting 404 Error in Your Spring Boot REST API

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 in arma.Arma, your controller in arma.Arma.RestController is 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/user to 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), use http://localhost:8080/myapp/api/user
  • 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/user will 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 like Mapped "{[/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 your build.gradle includes 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:28:39