SpringWebFlux Error with @EnableWebFlux annotation
Asked Answered
G

3

6

I'm using spring boot 2.1.1 version and use @EnableWebFlux but I got some errors.

Errors are

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.reactive.config.DelegatingWebFluxConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application

How can I fix this problem.

Girt answered 7/1, 2019 at 6:44 Comment(5)
Why are you manually enabling web flux? If that is on your class path Spring Boot will automatically enable it. The same applies to the web mvc part. Looks like you have both the spring-webmvc as well as the spring-webflux jars on your classpath trigger both to be configured.Perspire
Ok. But If I want to configure WebFlux , how can I do it. Via enabling @EnableWebFlux and exends from WebFluxConfigurer.class ?Girt
You don't need @EnableWebFlux as Spring Boot automatically does that for you. If you want to customize the configuration just create an WebFluxConfigurer in your project and add an @Configuration annotation to it.Perspire
Aha understood thanks a lotGirt
And also make sure you don't have both the web and WebFlux jars on your classpath else Spring Boot will attempt to configure both. Which as you already noticed, isn't allowed.Perspire
P
6

You can't have both Spring MVC and Spring WebFlux enabled in Spring SPR-16609

enabling MVC and WebFlux in the same application context which triggers a conflict

you can't have them in the same process currently.

It offers a workaround to use use reactive repositories:

However, you can use reactive repositories from your existing Spring MVC application, and return the reactive types (Flux or Mono), from Spring MVC controller methods.

Pharmacopsychosis answered 7/1, 2019 at 6:48 Comment(4)
I did not use SpringMvc .Girt
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>Girt
@NasibullohYandashev so remove @EnableWebFlux from your codePharmacopsychosis
@NasibullohYandashev If an answer helped you can accept it by click on V sign to the left of the answerPharmacopsychosis
W
0

Based on Spring Docs and SpringFramework Collaborator Juergen Hoeller, you can have both Spring MVC and Spring WebFlux present in the same app. In this way, you are using Spring MVC by default, while you can have reactively enabled endpoints at the same time. Keep in mind, it is essentially a servlet-based MVC app you are using.

If you also have @EnableWebFlux on top of that, you mean that you wish to strictly turn this into a reactive web application.

My suggestion is to turn off @EnableWebFlux and keep your application straightly MVC-based. You can still use some of the reactive features such as Mono<> and Flux<> by importing Webflux dependencies.

Weikert answered 1/4, 2021 at 6:48 Comment(0)
R
-2

I was facing the same issue as using io.springfox with Spring-WebFlux. I changed to use springdoc and solved the issue: https://springdoc.org/#migrating-from-springfox

For sample detail you can check in this: https://github.com/mnguyencntt/springboot-webflux-swagger

Refute answered 10/11, 2022 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.