From 373e2c51564a4c2cca306394ba52e5bcde839cfd Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sat, 22 Mar 2014 14:44:30 +0000 Subject: [PATCH] Allow user to opt out of ContentNegotiatingViewResolver The bean ID for the ContentNegotiatingViewResolver is now "viewResolver" (it is the *one*). The conditions have been changed so that a user only has to define a bean of the same name to switch it off. Fixes gh-546 --- .../autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java | 6 +++--- .../boot/autoconfigure/web/WebMvcAutoConfiguration.java | 4 ++-- spring-boot-docs/src/main/asciidoc/howto.adoc | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index bf829a6c10..fbaa5c67bd 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -182,9 +182,9 @@ public class ThymeleafAutoConfiguration { "excludedViewNames", String[].class)); resolver.setViewNames(this.environment.getProperty("viewNames", String[].class)); - // Needs to come before any fallback resolver (e.g. a - // InternalResourceViewResolver) - resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20); + // This resolver acts as a fallback resolver (e.g. like a + // InternalResourceViewResolver) so it needs to have low precedence + resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5); return resolver; } 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 49be49de5f..8039a13fb4 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 @@ -179,8 +179,8 @@ public class WebMvcAutoConfiguration { @Bean @ConditionalOnBean(ViewResolver.class) - public ContentNegotiatingViewResolver contentNegotiatingViewResolver( - BeanFactory beanFactory) { + @ConditionalOnMissingBean(name = "viewResolver") + public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager(beanFactory .getBean(ContentNegotiationManager.class)); diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index b6b11cd9e2..c2107e249c 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -747,9 +747,8 @@ added. client. There is a useful https://spring.io/blog/2013/06/03/content-negotiation-using-views[blog about `ContentNegotiatingViewResolver`] that you might like to study to learn more, and also look at the source code for detail. - Be careful not to define your own `ViewResolver` with id ``viewResolver'' (like the - `ContentNegotiatingViewResolver`) otherwise, in that case, your bean will be - overwritten, not the other way round. + You can switch off the auto-configured + `ContentNegotiatingViewResolver` by defining a bean named ``viewResolver''. * If you use Thymeleaf you will also have a `ThymeleafViewResolver` with id ``thymeleafViewResolver''. It looks for resources by surrounding the view name with a