6

I'm trying to use Netty server. So I excluded Tomcat in the pom.ml file;

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

When I run the application, I'm getting the error Caused by: java.lang.NoClassDefFoundError: javax/servlet/Filter

Then I add the dependency

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>compile</scope>
</dependency>

Then again when I run the application, I'm getting the error org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

I've checked several questions and no luck yet.

Any ideas?

Enviroment

  • Boot: 2.1.3.RELEASE
  • Eclipse: 4.7
williams
  • 61
  • 1
  • 1
  • 4
  • I tried with your pom.xml without adding the servlet dependency it started fine – Santosh b Mar 12 '19 at 15:34
  • Do you want to build a reactive reactive web api or a classic servlet-based api? This might be helpful: https://stackoverflow.com/questions/51377675/does-not-the-spring-boot-starter-web-and-spring-boot-starter-webflux-work-togeth – CryptoFool Mar 12 '19 at 15:34
  • @Santoshb, I executed the app in another computer and ran without issues. I guess something was corrupt in my local. Thanks! – williams Mar 12 '19 at 20:10
  • @Steve yes, my idea was to build an app to execute both, but i see now that is not the correct approach. Thanks! – williams Mar 12 '19 at 20:12

1 Answers1

9

If you want to use the netty server in WebFlux and WebFlux (spring-boot-starter-webflux) use by default netty server and spring-boot-starter-web use by default tomcat server.

WebFlux provides a choice of servers (Netty, Tomcat, Jetty, Undertow, and Servlet 3.1+ containers). Both Tomcat and Jetty are servlet based servers but Netty and Undertow are non-servlet based servers.

If you want to use netty server and external jar in which using servlet API then you have to add the below dependency in pom.xml

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
Suneet Khurana
  • 431
  • 5
  • 10