Skip to content

Raise error if both @EnableWebMvc and @EnableWebFlux used [SPR-16609] #21150

@spring-projects-issues

Description

@spring-projects-issues

Bjorn Harvold opened SPR-16609 and commented

Hi,

I just upgraded our existing application from Spring Boot 1.5.9 to Spring Boot 2 and Spring 5. I switched to Undertow and I wrote some reactive code. The tests and the app works fine locally.

In a nutshell, I added a new reactive configuration like so:

@Configuration
@EnableWebFlux
public class WebFluxConfig {

}

and then implemented some reactive Mongo repositories.

I already have an existing configuration for @EnableWebMvc.

@Configuration
@EnableWebMvc
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {
    private final ObjectMapper objectMapper;

    @Autowired
    public WebMvcConfig(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * Messages to support internationalization/localization.
     */
    @Bean(name = "messageSource")
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setUseCodeAsDefaultMessage(true);

        return messageSource;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("Configuring http message converters...");
        MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
        List<MediaType> jsonTypes = new ArrayList<>(3);
        jsonTypes.add(ApplicationMediaType.APPLICATION_JSON);
        jsonTypes.add(ApplicationMediaType.APPLICATION_TRAVELIKO_V1_JSON);
        jsonTypes.add(ApplicationMediaType.APPLICATION_TRAVELIKO_V2_JSON);
        jacksonConverter.setSupportedMediaTypes(jsonTypes);
        jacksonConverter.setObjectMapper(objectMapper);
        converters.add(jacksonConverter);

        List<MediaType> xmlTypes = new ArrayList<>(1);
        xmlTypes.add(ApplicationMediaType.APPLICATION_XML);
        Jaxb2RootElementHttpMessageConverter xmlConverter = new Jaxb2RootElementHttpMessageConverter();
        xmlConverter.setSupportedMediaTypes(xmlTypes);
        converters.add(xmlConverter);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory());
    }

    @Bean(name = "methodValidationPostProcessor")
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setValidator(localValidatorFactoryBean());

        return methodValidationPostProcessor;
    }

    @Bean(name = "localValidatorFactoryBean")
    public LocalValidatorFactoryBean localValidatorFactoryBean() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setMessageInterpolator(
                new ResourceBundleMessageInterpolator(
                        new MessageSourceResourceBundleLocator(messageSource())
                )
        );

        return localValidatorFactoryBean;
    }

    @Bean(name = "localeResolver")
    public LocaleResolver localeResolver() {
        return new CustomAcceptHeaderLocaleResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        log.info("Adding interceptors...");
        registry.addInterceptor(new DateTimeZoneHandlerInterceptor());
        registry.addInterceptor(new UserLocationHandlerInterceptor());
//        registry.addInterceptor(new LocaleChangeInterceptor()); // default paramName is 'locale' Not supported by out AcceptHeaderLocaleResolver
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//        log.info("Adding argument resolvers...");
        argumentResolvers.add(new DateTimeZoneHandlerMethodArgumentResolver());
        argumentResolvers.add(new LocationHandlerMethodArgumentResolver());
    }


}

The application has been in production for a couple of years already so the configuration is solid.

However, when I try to deploy this Spring Boot 2 / Undertow app to Google App Engine (JAR), I get this error:

build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 2018-03-18 14:36:48.909  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcUriComponentsContributor' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.method.support.CompositeUriComponentsContributor]: Factory method 'mvcUriComponentsContributor' threw exception; nested exception is java.lang.IllegalStateException: @Bean method WebMvcConfigurationSupport.requestMappingHandlerAdapter called as bean reference for type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] but overridden by non-compatible bean instance of type [org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter]. Overriding bean of same name declared in: class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 2018-03-18 14:36:49.257 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcUriComponentsContributor' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.method.support.CompositeUriComponentsContributor]: Factory method 'mvcUriComponentsContributor' threw exception; nested exception is java.lang.IllegalStateException: @Bean method WebMvcConfigurationSupport.requestMappingHandlerAdapter called as bean reference for type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] but overridden by non-compatible bean instance of type [org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter]. Overriding bean of same name declared in: class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at com.traveliko.platform.web.api.TravelikoWebApiApplication.main(TravelikoWebApiApplication.java:16) [classes!/:5.0.12]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [app.jar:5.0.12]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [app.jar:5.0.12]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [app.jar:5.0.12]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [app.jar:5.0.12]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.method.support.CompositeUriComponentsContributor]: Factory method 'mvcUriComponentsContributor' threw exception; nested exception is java.lang.IllegalStateException: @Bean method WebMvcConfigurationSupport.requestMappingHandlerAdapter called as bean reference for type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] but overridden by non-compatible bean instance of type [org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter]. Overriding bean of same name declared in: class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	... 26 common frames omitted
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: Caused by: java.lang.IllegalStateException: @Bean method WebMvcConfigurationSupport.requestMappingHandlerAdapter called as bean reference for type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] but overridden by non-compatible bean instance of type [org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter]. Overriding bean of same name declared in: class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:415) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:364) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e7b0733e.requestMappingHandlerAdapter(<generated>) ~[spring-webmvc-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.mvcUriComponentsContributor(WebMvcConfigurationSupport.java:857) ~[spring-webmvc-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e7b0733e.CGLIB$mvcUriComponentsContributor$36(<generated>) ~[spring-webmvc-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e7b0733e$$FastClassBySpringCGLIB$$a9b2a816.invoke(<generated>) ~[spring-webmvc-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e7b0733e.mvcUriComponentsContributor(<generated>) ~[spring-webmvc-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
build	18-Mar-2018 21:36:57	[�[1;34mINFO�[m] GCLOUD: 	... 27 common frames omitted

Affects: 5.0.4

Referenced from: commits 32faf09

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions