Can I extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?

I need to extend the WebMvcConfigurationSupport class to change two things:

@Configuration public class WebConfig extends WebMvcConfigurationSupport { @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping(); handlerMapping.setRemoveSemicolonContent(false); handlerMapping.setOrder(1); return handlerMapping; } } 

I like the default values โ€‹โ€‹registered from the WebMvcAutoConfiguration class, but due to the conditional annotation to the class, when I extend the WebMvcConfigurationSupport class, it prevents automatic configuration.

 @Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @Order(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) public class WebMvcAutoConfiguration {...} 

Do I need to load the class class WebMvcAutoConfiguration without having to essentially copy / paste most of the code in this class?

Or you can call RequestMappingHandlerMapping setOrder () and setRemoveSemicolonContent () from another place, so I can just use the @EnableWebMvc annotation and run the autoconfiguration class without any problems?

Thanks in advance!

+9
java spring spring-boot spring-mvc
Mar 08 '14 at 8:50
source share
4 answers

Your analysis is correct ( @EnableWebMvc or a direct extension to WebMvcConfigurationSupport will disable WebMvcAutoConfiguration ). I'm not sure what the alternative is, since a) we need a get-out clause for autoconfig, and b) I don't think Spring likes to have two WebMvcConfigurationSupports in the same context. We will discuss it with pleasure on github if you want to try to find a way to change it (maybe some kind of midpoint).

+6
Mar 08 '14 at 9:24
source share

I was able to configure RequestMappingHandlerMapping by saving WebMvcAutoConfiguration using BeanPostProcessor:

 @Configuration public class RequestMappingConfiguration { @Bean public RequestMappingHandlerMappingPostProcessor requestMappingHandlerMappingPostProcessor() { return new RequestMappingHandlerMappingPostProcessor(); } public static class RequestMappingHandlerMappingPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RequestMappingHandlerMapping) { ((RequestMappingHandlerMapping) bean).setUseSuffixPatternMatch(false); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } } } 

I would be happy if Spring Boot provided a better way to handle this ... maybe something could be done around PathMatchConfigurer ?

+8
Feb 18 '15 at 21:48
source share

Extend with DelegatingWebMvcConfiguration instead of WebMvcConfigurationSupport , this will not interfere with autoconfiguration:

 @Configuration public class WebConfig extends DelegatingWebMvcConfiguration { @Override public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping(); handlerMapping.setRemoveSemicolonContent(false); handlerMapping.setOrder(1); return handlerMapping; } } 
+4
Jun 15 '16 at 16:24
source share

I think the best way to do this in Spring Boot now is to add the WebMvcRegistrations component to your context - this solution did not exist at the time of your question (it is available with Spring Boot 1.4.0).

0
Nov 03 '17 at 18:19
source share



All Articles