Home > Net >  How to add @RestController to spring-webflux apps?
How to add @RestController to spring-webflux apps?

Time:04-07

The annotation @RestController cannot be resolved when only adding spring-boot-starter-webflux as maven dependency:

@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>

What is missing here? According to many resources out there (eg enter image description here

org.springframework:spring-web dependency is part of org.springframework.boot:spring-boot-starter-webflux jar so it should get resolved. You can check the complete dependency hierarchy by running mvn dependency:tree command from the terminal.

[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

If you are starting a new project you can use the spring initializer - https://start.spring.io/

  • Related