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

Spring Boot API调用返回404错误原因咨询

Troubleshooting 404 Error in Your Spring Boot Embedded Tomcat Deployment

Hey there, let's break down why your test API is returning a 404 after deploying your Spring Boot JAR to the embedded Tomcat. Based on the code snippet you shared, here are the most likely culprits:

1. Typo in Component Scan Path

Looking at your @ComponentScan annotation, you've got a partial package path: com.telematics.flee... — that's a missing "t" (should be com.telematics.fleet). This means Spring isn't scanning your controller package correctly, so it can't find and register your API endpoints. That's a classic reason for 404s!

Fix this by correcting the package path in your @ComponentScan (or even better, since @SpringBootApplication already includes component scanning for the main class's package and subpackages, you might not even need the explicit @ComponentScan if your controller is under com.telematics.fleet.controller). Here's the corrected main class code:

package com.telematics.fleet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.telematics.fleet.controller", "com.telematics.fleet"}) // Fixed the typo here
public class FleetApplication {
    public static void main(String[] args) {
        SpringApplication.run(FleetApplication.class, args);
    }
}

2. Missing or Incorrect Controller Annotations

Double-check your controller class to make sure it's annotated properly:

  • Use @RestController (combines @Controller and @ResponseBody) if you're building REST APIs.
  • Ensure your endpoint methods have the right @RequestMapping (or @GetMapping, @PostMapping, etc.) with the correct path. For example:
    @RestController
    @RequestMapping("/api/test")
    public class TestController {
        @GetMapping
        public String testEndpoint() {
            return "Hello from test API!";
        }
    }
    

3. Mismatched Request Path or HTTP Method

When testing the API, make sure you're using the exact path defined in your controller, including any leading slashes, and that the HTTP method (GET/POST/PUT/etc.) matches what you've annotated. For example, if your endpoint uses @PostMapping, a GET request will return 404.

4. Main Class Location

If you remove the explicit @ComponentScan, Spring Boot will automatically scan all packages under your main class's package (com.telematics.fleet). As long as your controller is in com.telematics.fleet.controller (a subpackage), this should work without extra configuration. So you could even simplify your main class to just:

package com.telematics.fleet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FleetApplication {
    public static void main(String[] args) {
        SpringApplication.run(FleetApplication.class, args);
    }
}

Give these fixes a try, and your 404 error should disappear once Spring can properly locate and register your API endpoints.

内容的提问来源于stack exchange,提问作者Vinayaka M Malya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:14:48