21

When I start to learn the spring-webflux, I have the question about this component.

I built a simple project, using maven to manage it. I addded the dependencies related to spring-boot-starter-web and spring-boot-starter-webflux, like :

    <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>
    </dependency>

But it doesn't work. When removing the spring-boot-starter-web dependency, it can work well.

CodeSlave
  • 425
  • 6
  • 21
kenzhaoyihui
  • 213
  • 1
  • 2
  • 4

2 Answers2

39

As explained in the Spring Boot reference documentation section about the web environment, adding both web and webflux starters will configure a Spring MVC web application.

This is behaving like that, because many existing Spring Boot web applications (using MVC) will depend on the webflux starter to use the WebClient. Spring MVC partially support reactive return types, so this is an expected use case. The opposite isn't really true, since a reactive application is not really likely to use Spring MVC bits.

So using both web and webflux starters is supported, but it will configure a Spring MVC application. You can always force the Spring Boot application to be reactive with:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

But it's still a good idea to clean up dependencies as it would be easy to use a blocking feature in your reactive web application.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • There is also property that allows achieve the same: https://stackoverflow.com/a/71234523/5562284 `spring.main.web-application-type=reactive` – nouveu Oct 17 '22 at 12:11
-1

I had a similar issue using spring-boot-starter-webflux and spring-data-geode causing

DEBUG [http-nio-8082-exec-2] org.sprin.web.servl.resou.ResourceHttpRequestHandler 454 handleRequest: Resource not found

It was resolved by changing the application type

@SpringBootApplication
public class Web {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Web.class);
        app.setWebApplicationType(WebApplicationType.REACTIVE);
        SpringApplication.run(Web.class, args);
    }
}

The whole class looks like this

enter image description here

After setting the application type, if I don't then call the SpringApplication in a static way, I get this:

Web

rupweb
  • 3,052
  • 1
  • 30
  • 57
  • 1
    are you sure you resolved something? because the `app` variable you declared actually is useless, as you run the Spring Boot app the traditional way (last line)... – maxxyme Sep 04 '20 at 08:45
  • it looks like you're right it looks like it should be `app.run` but maybe because the `SpringApplication.run` takes the class `Web.class` which is the name of the class where the `app` is defined, it uses that instead of a default. I edited the code above. – rupweb Sep 04 '20 at 09:25
  • that doesn't change anything; setting the `webApplicationType` to a variable that is not used by `SpringApplication.run()` is useless. `SpringApplication.run()` is a static method, so once Java runs it, the `app` variable is lost... – maxxyme Sep 04 '20 at 09:34
  • I updated the question again with what I saw and, yeah... oui... except the spring `run` does at least refer to the `Web.class` are you sure? – rupweb Sep 04 '20 at 09:39
  • `SpringApplication.run(Web.class, args);` where `Web.class` is the name of the class that declares a `SpringApplication app` of type `REACTIVE` and it works... `so don't fix it`?! – rupweb Sep 04 '20 at 09:47
  • because you have to write: `app.run(args);` not use the `Web.class` anymore! – maxxyme Sep 04 '20 at 09:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220965/discussion-between-rupweb-and-maxxyme). – rupweb Sep 04 '20 at 09:55