首页 > 解决方案 > 为什么 spring-boot-starter-webflux 会导入 spring-web?

问题描述

在我的pom.xml我替换了这个依赖:

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

经过

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

但是,我的 Spring Boot 应用程序仍然作为传统的“Web”应用程序运行,并且反应部分不起作用(例如org.springframework.web.server.WebFilter,从不调用实现类)。

我像这样运行应用程序:

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

这个答案https://stackoverflow.com/a/51378560说您可以通过执行以下操作强制您的 Spring Boot 应用程序作为 Reactive 运行:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

不幸的是,该setWebApplicationType()方法不是静态的。

所以我浏览了 Spring Boot 代码,发现webApplicationType是通过调用 enum 中的方法来确定的org.springframework.boot.WebApplicationType

就我而言,该static WebApplicationType deduceFromClasspath()方法继续存在,return WebApplicationType.SERVLET;因为在该方法的第一次测试中:

    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)评估true使整个谓词失败的结果。

所以我想知道,为什么WEBMVC_INDICATOR_CLASS(value = org.springframework.web.servlet.DispatcherServlet) 仍然存在???好吧,mvn dependency:tree给我答案:

[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

spring-boot-starter-webflux依赖项实际上都导入了!!!

那我该怎么办?!!排除spring-web?

编辑:根据要求,添加完整(编辑)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>

标签: spring-bootspring-mvcspring-webflux

解决方案


推荐阅读