Andrew
Andrew

Reputation: 57

Spring boot - executable war also deployable to app server

Let's say I have a spring boot web application - It is runnable via gradle (embedded tomcat). But I need it also to be possible to deploy war in standard way into app server. How the app should be configured? Standard web.xml along with xml configuration?

Currently I have something like:

@SpringBootApplication

public class MyApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
    System.setProperty("spring.profiles.active", "dev");
    SpringApplication.run(MyApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MyApplication.class);
}

@Configuration
@ConditionalOnWebApplication
public static class WebConfiguration {

    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> registerClientCookieConfigListener () {
        ServletListenerRegistrationBean<ServletContextListener> srb =
                new ServletListenerRegistrationBean<>();
        srb.setListener(new MyConfigListener());
        return srb;
    }

    @Bean
    public ServletListenerRegistrationBean<HttpSessionListener> registerMySessionConfigListener () {
        ServletListenerRegistrationBean<HttpSessionListener> srb =
                new ServletListenerRegistrationBean<>();
        srb.setListener(new MySessionConfigListener());
        return srb;
    }


    @Bean
    public FilterRegistrationBean registerLoginFilter() {
        FilterRegistrationBean filter = new FilterRegistrationBean(new MyFilter());
        filter.setUrlPatterns(Collections.singletonList("/*"));
        return filter;
    }

    @Bean
    public ServletRegistrationBean registerSAMLDispatcherServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(
                new DispatcherServlet(), "/test/*");
        bean.setLoadOnStartup(1);
        return bean;
    }
}

}

which is 1:1 mapping to web.xml. Is it even possible to deploy it to app server without web.xml?

Upvotes: 0

Views: 773

Answers (1)

Jayesh
Jayesh

Reputation: 999

You don't need web.xml to deploy spring boot to standalone tomcat server or any other web server.

spring boot does not rely on xml configurations, it configures an equivalent to the dispatcher servlet automatically.

to deploy a spring boot app to an another server, you need to update your packaging to war in maven

 <packaging>war</packaging>

and tell maven that a webserver will be available in the runtime and don't package it with scope provided

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

few documentations

https://www.baeldung.com/spring-boot-war-tomcat-deploy

https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

Upvotes: 1

Related Questions