2

in my pom.xml I replaced this dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

by

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

However, my Spring Boot application is still run as a traditional "web" application, and reactive parts do not work (e.g. a class implementing org.springframework.web.server.WebFilter is never called).

I run the app like this:

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

This answer https://stackoverflow.com/a/51378560 says you can force your Spring Boot app to be run as Reactive by doing this:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

Unfortunately the setWebApplicationType() method is not static.

So I browsed through the Spring Boot code and found out the webApplicationType is determined via calls to the methods in the enum org.springframework.boot.WebApplicationType.

In my case, the static WebApplicationType deduceFromClasspath() method continues to return WebApplicationType.SERVLET; because in the first test of the method:

    if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
        && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null))

ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null) evaluates to true which make the whole predicate fails.

So I wonder, why is the WEBMVC_INDICATOR_CLASS (value = org.springframework.web.servlet.DispatcherServlet) still present??? Well, mvn dependency:tree gives me the anwser:

[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.1.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:5.1.9.RELEASE:compile
[INFO] |  \- org.springframework:spring-webflux:jar:5.1.9.RELEA4SE:compile

The spring-boot-starter-webflux dependency actually imports both!!!

So how I am supposed to do?!! Exclude the spring-web?

Edit: as requested, added the full (redacted) pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>whatever-backend-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Whatever API</name>
    <description>API for whatever project</description>
    <url>https://api.company.com/</url>
    <organization>
        <name>Company</name>
        <url>http://www.company.com/</url>
    </organization>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <kotlin.version>1.3.10</kotlin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.scm.id>gitRepo</project.scm.id>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
        <springfox.version>2.9.2</springfox.version>
        <graphql-spring-boot-starter.version>5.10.0</graphql-spring-boot-starter.version>
    </properties>

    <distributionManagement>
        <repository>
            <id>releasesRepo</id>
            <url>https://nexus.company.com/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <uniqueVersion>false</uniqueVersion>
            <id>snapshotsRepo</id>
            <url>https://nexus.company.com/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <scm>
        <developerConnection>scm:git:https://gitlab.company.com/root/dev/whatever/whatever-backend-api.git</developerConnection>
        <tag>HEAD</tag>
    </scm>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
        </dependency>

        <dependency>
            <groupId>com.kastkode</groupId>
            <artifactId>springsandwich</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>${graphql-spring-boot-starter.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-spring-boot-starter-test</artifactId>
            <version>${graphql-spring-boot-starter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>whatever-api</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                        <configuration>
                            <additionalProperties>
                                <java.source>${java.version}</java.source>
                                <encoding.source>${project.build.sourceEncoding}</encoding.source>
                                <encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting>
                            </additionalProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <configuration>
                    <excludes>
                        <exclude>*MethodAccess</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
maxxyme
  • 2,164
  • 5
  • 31
  • 48
  • is it the same with spring-boot-starter-webflux:2.2.4.RELEASE because it looks like your maven is picking up 2.1.8 version of webflux. Latest version is? – rupweb Sep 04 '20 at 10:06
  • We currently use Spring Boot 2.1.8.RELEASE because we can't afford to upgrade to newer versions, but according to MVN repository, it still does: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux/2.2.4.RELEASE – maxxyme Sep 04 '20 at 10:13
  • the webflux version is backward compatible though? latest version is 2.3.3.RELEASE. I would try pointing your maven to this version? Then again I see that the 2.3.3. release also uses spring-web. I don't know why! it needs it?!! Can you try setting your app to REACTIVE and running it as Static but then referring back to your control class, like I did [here](https://stackoverflow.com/questions/51377675/dont-spring-boot-starter-web-and-spring-boot-starter-webflux-work-together). LMK if it worked? – rupweb Sep 04 '20 at 10:25
  • can you please post your full pom – Toerktumlare Sep 04 '20 at 10:47
  • @ThomasAndolf added it! – maxxyme Sep 04 '20 at 11:59
  • 2
    `2.9.2` does not support webflux, this is one of your problems – Toerktumlare Sep 04 '20 at 12:31
  • 1
    https://mvnrepository.com/artifact/io.springfox/springfox-swagger2/3.0.0 – Toerktumlare Sep 04 '20 at 12:41

0 Answers0