Skip to content

Commit

Permalink
Make RequestMappingHandlerMapping @primary
Browse files Browse the repository at this point in the history
Update WebMvcAutoConfiguration so that the RequestMappingHandlerMapping
bean is @primary. Prior to this commit a NoUniqueBeanDefinitionException
would be thrown then using the MvcUriComponentsBuilder.

Fixes gh-2237
  • Loading branch information
philwebb committed Dec 26, 2014
1 parent ed1db85 commit 96d479c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -25,6 +25,7 @@
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
Expand Down Expand Up @@ -54,8 +55,10 @@
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -229,6 +232,16 @@ public void portPropertiesOnDifferentPort() throws Exception {
assertAllClosed();
}

@Test
public void singleRequestMappingInfoHandlerMappingBean() throws Exception {
this.applicationContext.register(RootConfig.class, BaseConfiguration.class,
ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class);
this.applicationContext.refresh();
RequestMappingInfoHandlerMapping mapping = this.applicationContext
.getBean(RequestMappingInfoHandlerMapping.class);
assertThat(mapping, not(instanceOf(EndpointHandlerMapping.class)));
}

private void assertAllClosed() throws Exception {
assertContent("/controller", ports.get().server, null);
assertContent("/endpoint", ports.get().server, null);
Expand Down
Expand Up @@ -41,6 +41,8 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter;
Expand All @@ -62,13 +64,15 @@
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
Expand Down Expand Up @@ -127,7 +131,7 @@ public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
// Defined as a nested config to ensure WebMvcConfigurerAdapter is not read when not
// on the classpath
@Configuration
@EnableWebMvc
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {

Expand Down Expand Up @@ -304,4 +308,20 @@ public ResourceHttpRequestHandler faviconRequestHandler() {

}

/**
* Configuration equivalent to {@code @EnableWebMvc}.
*/
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {

@Bean
@Primary
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
// Must be @Primary for MvcUriComponentsBuilder to work
return super.requestMappingHandlerMapping();
}

}

}

0 comments on commit 96d479c

Please sign in to comment.