40

I would like to add resource handlers. In the forum they use WebMvcConfigurationSupport: http://forum.springsource.org/showthread.php?116068-How-to-configure-lt-mvc-resources-gt-mapping-to-take-precedence-over-RequestMapping&p=384066#post384066

and docs say WebMvcConfigurerAdapter: http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html

What's the difference and which one to use? Both has the addResourceHandlers method I need.

This is my current class:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    public @Override void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
    }

    public @Bean TilesViewResolver tilesViewResolver() {
        return new TilesViewResolver();
    }

    public @Bean TilesConfigurer tilesConfigurer() {
        TilesConfigurer ret = new TilesConfigurer();
        ret.setDefinitions(new String[] { "classpath:tiles.xml" });
        return ret;
    }
}
dtrunk
  • 4,685
  • 17
  • 65
  • 109

4 Answers4

26

The answer is in the doc you referenced above:

If the customization options of WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods

In short, if @EnableWebMvc works for you, there is no need to look any further.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
7

if you use ConfigurationSupport class get ready mind numbing hardwork when trying to serve static resources, because it does not work.

user1363516
  • 360
  • 4
  • 15
  • I'm seeing the exact same thing.. is there a workaround here? Adding resourcehandlers don't seem to have any effect, they seem to be ignored, or at least that's what I'm seeing.. – Mark D Nov 19 '15 at 10:40
  • 1
    @MarkD use WebMvcConfigurerAdapter and it will work flawlessly – user1363516 Nov 24 '15 at 15:50
  • 1
    ya except I need to use some features not in it - i.e. override the requestMappingHandlerMapping() method which doesn't seem to be in the ConfigurerAdapter? – Mark D Nov 25 '15 at 18:15
7

I was recently solving this very same problem when configuring converters and it resulted in quite a long post.

By default Spring Boot uses its implementation of WebMvcConfigurationSupport and does a lot of auto-magic including finding all the WebMvcConfigurer and using them. There is one implementation provided by Boot already and you may add more. This results in seemingly confusing behaviour when the list of converters coming to configureMessageConverters in your implementation of WebMvcConfigurer is already pre-populated from previous configurer.

These types (WebMvcConfigurationSupport and WebMvcConfigurer) have also strikingly similar interface - but the first does NOT implement the other. The point is:

Support class searches for configurers and uses them + does something on its own.

If you extend from WebMvcConfigurationSupport you take over the configuration and while there are some things available that are not in WebMvcConfigurer (like addDefaultHttpMessageConverters) there is also tons of code from EnableWebMvcConfiguration and DelegatingWebMvcConfiguration that does not happen.

Both extending WebMvcConfigurationSupport or WebMvcConfigurer (not sure both at once makes much sense) have their valid usages, but with extending the support class you take over the process much more and lose a lot of "opinionated" Spring Boot functionality.

virgo47
  • 2,283
  • 24
  • 30
3

Its better to extend WebMvcConfigurationSupport. It provides more customization options and also works fine with

configureMessageConverters(List<HttpMessageConverter<?>> converters) 

cause you can add these convertors using

addDefaultHttpMessageConverters(converters);

that is not available with WebMvcConfigurerAdapter.

Click [here] How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?

If you extend WebMvcConfigurerAdapter, it behaves strangely with configuring Jackson and Jaxb. That happened with me !!!

Community
  • 1
  • 1
Pushpendra Jaiswal
  • 450
  • 1
  • 3
  • 15
  • Yes you are right. Same problems here ... because alle the common converters are missing without calling `addDefaultHttpMessageConverters(converters);` – Pascal Jul 16 '16 at 16:16
  • This depends really. If you extend from `WebMvcConfigurationSupport` many things implemented in the default Spring Boot subclasses stop working. E.g. beans are not included automatically. – virgo47 May 15 '20 at 07:13