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

Spring Boot Jar转War部署后无法访问Controller包内服务求助

解决Spring Boot War包部署Tomcat后Controller接口无法访问的问题

Hey VijayS, let's figure out why your Controller package endpoints aren't working while the /test endpoint in your main class is accessible. First off, you almost never need to manually configure DispatcherServlet for a Spring Boot War deployment—Spring Boot handles that automatically, but there are a few common missteps that could be causing this issue. Let's go through the most likely fixes:

1. 检查启动类的包扫描范围

Spring Boot’s @SpringBootApplication annotation includes @ComponentScan, which by default scans the package containing your main class and all its sub-packages. If your Controller, Service, or Repo packages aren’t nested under your main class’s package, Spring won’t detect those components at all.

  • Fix: Either move your Controller/Service/Repo packages into a sub-package of your main class’s package, or explicitly define the base packages to scan in @SpringBootApplication:
    @SpringBootApplication(scanBasePackages = {"com.yourproject.snservice.controller", "com.yourproject.snservice.service", "com.yourproject.snservice.repo"})
    public class SnServiceApplication extends SpringBootServletInitializer {
        // ... your existing code
    }
    

2. 确认War包的POM配置

Make sure your pom.xml is set up correctly for War packaging, especially excluding the embedded Tomcat (since you’re using an external Tomcat server):

<!-- Set packaging to war -->
<packaging>war</packaging>

<dependencies>
    <!-- Exclude embedded Tomcat so it doesn't conflict with the external one -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- Add your other dependencies here (spring-boot-starter-web, etc.) -->
</dependencies>

Also, double-check that your main class properly extends SpringBootServletInitializer and overrides the configure method—this is required for War deployment:

@SpringBootApplication
public class SnServiceApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SnServiceApplication.class);
    }

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

    // Your working /test endpoint
    @GetMapping("/test")
    public String test() {
        return "Test successful!";
    }
}

3. 验证Controller的注解和请求映射

  • Ensure all your Controller classes are annotated with @RestController (for JSON/REST endpoints) or @Controller (if using view templates). Without these annotations, Spring won’t register them as request handlers.
  • Double-check your request mappings: If your Controller has @GetMapping("/users"), the full accessible URL should be localhost:8080/snservice/users (since snservice is your Tomcat context path, derived from your War file name).
  • Example of a correctly annotated Controller:
    @RestController
    @RequestMapping("/api") // Optional base path
    public class UserController {
        @Autowired
        private UserService userService;
    
        @GetMapping("/users")
        public List<User> getAllUsers() {
            return userService.getAllUsers();
        }
    }
    

4. 确认Tomcat的上下文路径

When you deploy your War file to Tomcat, the default context path is the name of the War file. So if your War is named snservice.war, all endpoints are prefixed with localhost:8080/snservice/. Make sure you’re not missing this prefix when testing your Controller endpoints.

5. 启用调试日志排查

If none of the above works, enable debug logging to see if your Controllers are being registered by Spring. Add these lines to your application.properties:

logging.level.org.springframework.context.annotation=DEBUG
logging.level.org.springframework.web.servlet.mvc.method.annotation=DEBUG

Then check Tomcat’s catalina.out log file (usually in tomcat/logs/). Look for lines indicating that your Controllers are being scanned and mapped to URLs—if you don’t see them, the issue is definitely with component scanning.

If you’re still stuck, sharing your exact package structure (e.g., main class in com.snservice, Controller in com.snservice.controller vs. com.controller) and a snippet of one of your Controller classes would help narrow things down further.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:21:32