Home > OS >  How to deploy a Spring-Boot-Webflux application to Tomcat standalone server?
How to deploy a Spring-Boot-Webflux application to Tomcat standalone server?

Time:03-10

A normal spring-web application can be deployed to tomcat standalone as war file as follows:

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {

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

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

Question: how can I deploy such an application after migrating to spring-webflux to tomcat?

Docs say: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-httphandler

To deploy as a WAR to any Servlet 3.1 container, you can extend and include AbstractReactiveWebInitializer in the WAR. That class wraps an HttpHandler with ServletHttpHandlerAdapter and registers that as a Servlet.

So but there is no example how to.

I tried as follows, which gives an exception:

@SpringBootApplication
public class MyApplication extends AbstractReactiveWebInitializer {

    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[] {MyApplication.class};
    }

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

Result:

MyApplication.java:13:8
java: cannot access javax.servlet.ServletException
  class file for javax.servlet.ServletException not found

CodePudding user response:

You should follow the Spring Boot Guide on Traditional Deployment. Which explains that you would need spring-boot-starter-tomcat (as that is the server of your choice) with the scope provided. Else it might start adding additional jars you don't need and which might (and probably will) interfere with deployment.

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

You can also run the generated WAR file as a regular application. So you can build the WAR and just do java -jar your.war and it will start. Or you can just run the @SpringBootApplication annotated class (although if you use JSP that might not work 100%, in my experience).

  • Related