Skip to content

Commit 4f6860f

Browse files
committed
Merge pull request #40874 from rohitp-a
* gh-40874: Polish "Make it easier to override RequestToViewNameTranslator bean" Make it easier to override RequestToViewNameTranslator bean Closes gh-40874
2 parents 70fd788 + 7e04ac2 commit 4f6860f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import org.springframework.web.servlet.FlashMapManager;
9696
import org.springframework.web.servlet.HandlerExceptionResolver;
9797
import org.springframework.web.servlet.LocaleResolver;
98+
import org.springframework.web.servlet.RequestToViewNameTranslator;
9899
import org.springframework.web.servlet.View;
99100
import org.springframework.web.servlet.ViewResolver;
100101
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
@@ -469,6 +470,13 @@ public FlashMapManager flashMapManager() {
469470
return super.flashMapManager();
470471
}
471472

473+
@Override
474+
@Bean
475+
@ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME)
476+
public RequestToViewNameTranslator viewNameTranslator() {
477+
return super.viewNameTranslator();
478+
}
479+
472480
private Resource getIndexHtmlResource() {
473481
for (String location : this.resourceProperties.getStaticLocations()) {
474482
Resource indexHtml = getIndexHtmlResource(location);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import org.springframework.web.servlet.HandlerExceptionResolver;
103103
import org.springframework.web.servlet.HandlerMapping;
104104
import org.springframework.web.servlet.LocaleResolver;
105+
import org.springframework.web.servlet.RequestToViewNameTranslator;
105106
import org.springframework.web.servlet.View;
106107
import org.springframework.web.servlet.ViewResolver;
107108
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
@@ -136,6 +137,7 @@
136137
import org.springframework.web.servlet.support.SessionFlashMapManager;
137138
import org.springframework.web.servlet.view.AbstractView;
138139
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
140+
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
139141
import org.springframework.web.util.UrlPathHelper;
140142

141143
import static org.assertj.core.api.Assertions.assertThat;
@@ -404,6 +406,26 @@ void customFlashMapManagerWithDifferentNameDoesNotReplaceDefaultFlashMapManager(
404406
});
405407
}
406408

409+
@Test
410+
void customViewNameTranslatorWithMatchingNameReplacesDefaultViewNameTranslator() {
411+
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
412+
.run((context) -> {
413+
assertThat(context).hasSingleBean(RequestToViewNameTranslator.class);
414+
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
415+
});
416+
}
417+
418+
@Test
419+
void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() {
420+
this.contextRunner
421+
.withBean("customViewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
422+
.run((context) -> {
423+
assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
424+
assertThat(context.getBean("viewNameTranslator"))
425+
.isInstanceOf(DefaultRequestToViewNameTranslator.class);
426+
});
427+
}
428+
407429
@Test
408430
void defaultDateFormat() {
409431
this.contextRunner.run((context) -> {
@@ -1458,6 +1480,15 @@ protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest requ
14581480

14591481
}
14601482

1483+
static class CustomViewNameTranslator implements RequestToViewNameTranslator {
1484+
1485+
@Override
1486+
public String getViewName(HttpServletRequest requestAttributes) {
1487+
return null;
1488+
}
1489+
1490+
}
1491+
14611492
@Configuration(proxyBeanMethods = false)
14621493
static class ResourceHandlersWithChildAndParentContextConfiguration {
14631494

0 commit comments

Comments
 (0)