From 062ed4ba2b01368170d82f9618317adb09a8d176 Mon Sep 17 00:00:00 2001 From: rohit patidar Date: Wed, 22 May 2024 16:28:54 +0100 Subject: [PATCH 1/2] Make it easier to override RequestToViewNameTranslator bean See gh-40874 --- .../web/servlet/WebMvcAutoConfiguration.java | 10 +++++++ .../servlet/WebMvcAutoConfigurationTests.java | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index f5fff60f90a6..29aa7e3fdc3b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -95,6 +95,7 @@ import org.springframework.web.servlet.FlashMapManager; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.RequestToViewNameTranslator; import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; @@ -469,6 +470,15 @@ public FlashMapManager flashMapManager() { return super.flashMapManager(); } + + @Override + @Bean + @ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) + public RequestToViewNameTranslator viewNameTranslator() { + return super.viewNameTranslator(); + } + + private Resource getIndexHtmlResource() { for (String location : this.resourceProperties.getStaticLocations()) { Resource indexHtml = getIndexHtmlResource(location); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 7ccc5e12bff4..cd6ce32565cc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -65,6 +65,7 @@ import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -89,6 +90,7 @@ import org.springframework.web.accept.ParameterContentNegotiationStrategy; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; +import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.FormContentFilter; @@ -102,6 +104,7 @@ import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.RequestToViewNameTranslator; import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; @@ -404,6 +407,24 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager( }); } + @Test + public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { + this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) + .run((context) -> { + assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class); + }); + } + + @Test + void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() { + this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) + .run((context) -> { + assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); + }); + } + @Test void defaultDateFormat() { this.contextRunner.run((context) -> { @@ -1458,6 +1479,15 @@ protected void updateFlashMaps(List flashMaps, HttpServletRequest requ } + static class CustomViewNameTranslator implements RequestToViewNameTranslator { + + @Override + public String getViewName(HttpServletRequest requestAttributes) { + return null; + } + + } + @Configuration(proxyBeanMethods = false) static class ResourceHandlersWithChildAndParentContextConfiguration { From 7e04ac2967b0bb3562b727d94a684a3d04136142 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 27 Jun 2024 15:26:35 +0100 Subject: [PATCH 2/2] Polish "Make it easier to override RequestToViewNameTranslator bean" See gh-40874 --- .../web/servlet/WebMvcAutoConfiguration.java | 2 -- .../servlet/WebMvcAutoConfigurationTests.java | 23 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index 29aa7e3fdc3b..937d4b4f0edf 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -470,7 +470,6 @@ public FlashMapManager flashMapManager() { return super.flashMapManager(); } - @Override @Bean @ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) @@ -478,7 +477,6 @@ public RequestToViewNameTranslator viewNameTranslator() { return super.viewNameTranslator(); } - private Resource getIndexHtmlResource() { for (String location : this.resourceProperties.getStaticLocations()) { Resource indexHtml = getIndexHtmlResource(location); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index cd6ce32565cc..6fb28fe2c90a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -65,7 +65,6 @@ import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -90,7 +89,6 @@ import org.springframework.web.accept.ParameterContentNegotiationStrategy; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; -import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.FormContentFilter; @@ -139,6 +137,7 @@ import org.springframework.web.servlet.support.SessionFlashMapManager; import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; +import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator; import org.springframework.web.util.UrlPathHelper; import static org.assertj.core.api.Assertions.assertThat; @@ -408,21 +407,23 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager( } @Test - public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { + void customViewNameTranslatorWithMatchingNameReplacesDefaultViewNameTranslator() { this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) .run((context) -> { - assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); - assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class); + assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); }); } @Test - void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() { - this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) - .run((context) -> { - assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); - assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); - }); + void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { + this.contextRunner + .withBean("customViewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) + .run((context) -> { + assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")) + .isInstanceOf(DefaultRequestToViewNameTranslator.class); + }); } @Test