SpringWebFlux Error with @EnableWebFlux annotation

Viewed 8071

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.

2 Answers

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.

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.

Related