diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index d91527072f..7507e87a1f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -190,7 +190,7 @@ public class WebMvcAutoConfiguration { @Bean @ConditionalOnBean(ViewResolver.class) - @ConditionalOnMissingBean(name = "viewResolver") + @ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class) public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager(beanFactory diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index a3012be685..852e1da9dd 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -52,6 +52,7 @@ import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerMapping; 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.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; @@ -59,6 +60,7 @@ import org.springframework.web.servlet.i18n.FixedLocaleResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.view.AbstractView; +import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -308,6 +310,31 @@ public class WebMvcAutoConfigurationTests { ReflectionTestUtils.getField(adapter, "ignoreDefaultModelOnRedirect")); } + @Test + public void customViewResolver() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(Config.class, CustomViewResolver.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBean("viewResolver"), instanceOf(MyViewResolver.class)); + } + + @Test + public void customContentNegotiatingViewResolver() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(Config.class, CustomContentNegotiatingViewResolver.class, + WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + Map beans = this.context + .getBeansOfType(ContentNegotiatingViewResolver.class); + assertThat(beans.size(), equalTo(1)); + assertThat(beans.keySet().iterator().next(), equalTo("myViewResolver")); + } + @Configuration protected static class ViewConfig { @@ -362,4 +389,33 @@ public class WebMvcAutoConfigurationTests { } + @Configuration + public static class CustomViewResolver { + + @Bean + public ViewResolver viewResolver() { + return new MyViewResolver(); + } + + } + + @Configuration + public static class CustomContentNegotiatingViewResolver { + + @Bean + public ContentNegotiatingViewResolver myViewResolver() { + return new ContentNegotiatingViewResolver(); + } + + } + + private static class MyViewResolver implements ViewResolver { + + @Override + public View resolveViewName(String viewName, Locale locale) throws Exception { + return null; + } + + } + }