Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading