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

Spring Boot访问REST API接口出现Http 404错误,求助解决

Troubleshooting the 404 Error for Your Spring Boot REST API

Let’s break down why you’re seeing that 404 Whitelabel Error Page when hitting your /greeting endpoint, and how to fix it step by step.

1. Verify Your Controller Annotations Are Correct

First up: make sure your controller class is properly annotated so Spring recognizes it as a REST endpoint handler. The official guide uses @RestController—this is a shortcut that combines @Controller and @ResponseBody, which tells Spring to return the method’s response directly as HTTP body content.

If you forgot @RestController (or used only @Controller without @ResponseBody on your method), Spring won’t map the endpoint correctly. Double-check your controller code looks something like this:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting() {
        return "Hello, World!";
    }
}

2. Confirm Your Request Mapping Path Matches

Next, ensure the path in your @GetMapping (or @RequestMapping) annotation exactly matches what you’re accessing: /greeting. Typos like /greetings (plural) or extra slashes (//greeting) will trigger a 404. Also, check that you’re using the right HTTP method—if you annotated the method with @PostMapping but tried to access it via a GET request, you’ll get this error too.

3. Check Your Spring Boot Application Class Location

Spring Boot’s @SpringBootApplication annotation automatically scans for components (like controllers) in its own package and all sub-packages. If your controller class is in a package that’s above your application class’s package, Spring won’t find it.

For example:

  • If your application class is in com.example.demo, your controller should be in com.example.demo or com.example.demo.controllers (a sub-package).
  • If your controller is in com.example.api, you’ll need to add a @ComponentScan annotation to your application class to include that package:
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    @ComponentScan(basePackages = {"com.example.demo", "com.example.api"})
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

4. Ensure Your Controller Is Being Compiled and Loaded

Sometimes, build issues can prevent your controller class from being compiled into the target/classes directory. Run mvn clean install (if using Maven) or ./gradlew clean build (if using Gradle) to rebuild your project, then check if your GreetingController.class file exists in the output directory. If it’s missing, fix any compilation errors in your code first.

5. Verify the Application Is Running on the Correct Port

Double-check that your Spring Boot app is running on port 8080 (the default). Look for a line like this in your startup logs:

Tomcat started on port(s): 8080 (http) with context path ''

If you’ve changed the port in application.properties or application.yml (e.g., server.port=8081), you’ll need to update your request URL to match the correct port.

Final Check: Look for Mapping Logs

If you still can’t resolve the issue, enable debug logging for Spring Web to see if your endpoint is being mapped. Add this to application.properties:

logging.level.org.springframework.web=DEBUG

When you start the app, look for a log line like:

Mapped "{[/greeting]}" onto public java.lang.String com.example.demo.GreetingController.greeting()

If you don’t see this line, it confirms Spring isn’t registering your controller—go back to checking package scanning and annotations.


内容的提问来源于stack exchange,提问作者user9520057

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:53:06