Search code examples
javaspring-boottomcatjava-8tomcat10

How to deploy war file on tomcat application server


I have runnable springboot (2.4) Java (8) project and it works fine with embedded tomcat (10) with .jar extension. I need to run the same project on the tomcat application server but after I changed it into .war and deployed into tomcat application server it gives 404 error message. Is there anything I need to do additional works to deploy on tomcat applications server?

My main class as per below,

@ComponentScan("com.example.testproject")
@SpringBootApplication
@EnableSwagger2
@Slf4j
@EnableAutoConfiguration
public class ApplicationStarter extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return configureApplication(builder);
    }
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ApplicationStarter.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
    private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {    
        return builder.sources(ApplicationStarter.class);
    }
}

and I added below dependency also,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

<groupId>com.example</groupId>
   <artifactId>testproject</artifactId>
   <version>0.0.1-testproject</version>
<packaging>war</packaging> // earlier it was jar and with embedded tomcat its working 

Solution

  • You should also say that tomcat will be provided in your pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>