如何将@RestController 添加到 spring-webflux 应用程序?

How to add @RestController to spring-webflux apps?

只添加spring-boot-starter-webflux作为maven依赖无法解析注解@RestController:

@RestController
public class MyController {

}

pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
    <relativePath/>
</parent>

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

这里缺少什么?根据那里的许多资源(例如 https://medium.com/javarevisited/basic-introduction-to-spring-webflux-eb155f501b17),webflux 依赖项对于 spring-boot.

中的 webflux-webservice 应该足够了

RestController 注释是 org.springframework 的一部分:spring-web:依赖项

org.springframework:spring-web 依赖是 org.springframework.boot:spring-boot-starter-webflux[= 的一部分25=] jar 所以它应该得到解决。您可以从终端通过 运行 mvn dependency:tree 命令检查完整的依赖层次结构。

[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.6.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.6.6:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.6.6:compile
[INFO] |  |  |  \- org.springframework:spring-context:jar:5.3.18:compile
[INFO] |  |  |     +- org.springframework:spring-aop:jar:5.3.18:compile
[INFO] |  |  |     \- org.springframework:spring-expression:jar:5.3.18:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.6.6:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.6.6:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.29:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.6.6:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.2.2:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.2:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.13.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.2:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:2.6.6:compile
[INFO] |  |  \- io.projectreactor.netty:reactor-netty-http:jar:1.0.17:compile
[INFO] |  |     +- io.netty:netty-codec-http:jar:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-common:jar:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-buffer:jar:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-transport:jar:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-codec:jar:4.1.75.Final:compile
[INFO] |  |     |  \- io.netty:netty-handler:jar:4.1.75.Final:compile
[INFO] |  |     +- io.netty:netty-codec-http2:jar:4.1.75.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns:jar:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-resolver:jar:4.1.75.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec-dns:jar:4.1.75.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns-native-macos:jar:osx-x86_64:4.1.75.Final:compile
[INFO] |  |     |  \- io.netty:netty-resolver-dns-classes-macos:jar:4.1.75.Final:compile
[INFO] |  |     +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.75.Final:compile
[INFO] |  |     |  +- io.netty:netty-transport-native-unix-common:jar:4.1.75.Final:compile
[INFO] |  |     |  \- io.netty:netty-transport-classes-epoll:jar:4.1.75.Final:compile
[INFO] |  |     \- io.projectreactor.netty:reactor-netty-core:jar:1.0.17:compile
[INFO] |  |        \- io.netty:netty-handler-proxy:jar:4.1.75.Final:compile
[INFO] |  |           \- io.netty:netty-codec-socks:jar:4.1.75.Final:compile
[INFO] |  +- org.springframework:spring-web:jar:5.3.18:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.3.18:compile
[INFO] |  \- org.springframework:spring-webflux:jar:5.3.18:compile

如果您正在开始一个新项目,您可以使用 spring 初始化程序 - https://start.spring.io/