'How to deploy a Spring-Boot-Webflux application to Tomcat standalone server?

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


Solution 1:[1]

This use case is not supported by the Spring Boot team, as explained in the reference documentation. Even if some features might work, you'll find many limitations and bugs to this approach - and it seems you've started to experience just this.

Solution 2:[2]

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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brian Clozel
Solution 2 M. Deinum