Skip to content

Commit 72afdc6

Browse files
committed
Reorder WebMvcConfigurer from auto-configuration
Prior to this commit, all `WebMvcConfigurer` instances provided by user configuration were processed *before* the one provided by the `WebMvcAutoConfiguration`. For many options this has no consequence, but for some, like the `ContentNegotiationConfigurer`, settings were overriden by the auto-configuration even if developers provided an opinion. This commit orders the `WebMvcConfigurer` provided by the auto-configuration at `0`, so that custom configurers (unordered, at `Ordered.LOWEST_PRECEDENCE`) are processed *after*. This still gives room to developers for configuring things *before* the auto-configuration - they can still order their own configuration accordingly. Fixes gh-12389
1 parent 9b1003d commit 72afdc6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.springframework.context.annotation.Lazy;
6767
import org.springframework.context.annotation.Primary;
6868
import org.springframework.core.Ordered;
69+
import org.springframework.core.annotation.Order;
6970
import org.springframework.core.convert.converter.Converter;
7071
import org.springframework.core.convert.converter.GenericConverter;
7172
import org.springframework.core.io.ClassPathResource;
@@ -168,6 +169,7 @@ public OrderedHttpPutFormContentFilter httpPutFormContentFilter() {
168169
@Configuration
169170
@Import(EnableWebMvcConfiguration.class)
170171
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
172+
@Order(0)
171173
public static class WebMvcAutoConfigurationAdapter
172174
implements WebMvcConfigurer, ResourceLoaderAware {
173175

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

+22
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.springframework.web.servlet.LocaleResolver;
7272
import org.springframework.web.servlet.View;
7373
import org.springframework.web.servlet.ViewResolver;
74+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
7475
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
7576
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7677
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
@@ -809,6 +810,18 @@ public void queryParameterContentNegotiation() {
809810
});
810811
}
811812

813+
@Test
814+
public void customConfigurerAppliedAfterAutoConfig() {
815+
this.contextRunner
816+
.withUserConfiguration(CustomConfigurer.class)
817+
.run((context) -> {
818+
ContentNegotiationManager manager = context.getBean(ContentNegotiationManager.class);
819+
assertThat(manager.getStrategies()).anyMatch(strategy ->
820+
WebMvcAutoConfiguration.OptionalPathExtensionContentNegotiationStrategy.class
821+
.isAssignableFrom(strategy.getClass()));
822+
});
823+
}
824+
812825
private void assertCacheControl(AssertableWebApplicationContext context) {
813826
Map<String, Object> handlerMap = getHandlerMap(
814827
context.getBean("resourceHandlerMapping", HandlerMapping.class));
@@ -1086,4 +1099,13 @@ public HttpMessageConverter<?> customHttpMessageConverter(
10861099

10871100
}
10881101

1102+
@Configuration
1103+
static class CustomConfigurer implements WebMvcConfigurer {
1104+
1105+
@Override
1106+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
1107+
configurer.favorPathExtension(true);
1108+
}
1109+
}
1110+
10891111
}

0 commit comments

Comments
 (0)