7

In traditional Spring MVC, I can extend WebMvcConfigurationSupport and do the following:

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
    favorParameter(true).
    defaultContentType(MediaType.APPLICATION_JSON).
    mediaType("xml", MediaType.APPLICATION_XML);
}

How do I do this in a Spring Boot app? My understanding is that adding a WebMvcConfigurationSupport with @EnableWebMvc will disable the Spring Boot WebMvc autoconfigure, which I don't want.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219

2 Answers2

9

Per Spring Boot reference on auto configuration and Spring MVC:

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.

For example if you want to keep Spring Boot's auto configuration, and customize ContentNegotiationConfigurer:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

   ...
   @Override
   public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
      super.configureContentNegotiation(configurer);
      configurer.favorParameter(..);
      ...
      configurer.defaultContentType(..);
   }
}
ikumen
  • 11,275
  • 4
  • 41
  • 41
  • 1
    You're right and I knew that but the class I'm currently extending is not `WebMvcConfigurerAdapter` but `WebMvcConfigurationSupport`. The later provides configuration options the former doesn't. – Abhijit Sarkar May 10 '16 at 06:25
  • I don't think it's possible to use any part of Spring Boot's WebMvcAutoConfiguration and provide your own WebMvcConfigurationSupport as Spring Boot will only auto configure if there's no WebMvcConfigurationSupport. I think one of the advantages of using Spring Boot is the auto configuration. I would try to use WebMvcConfigurerAdapter, and for options that are NOT supported use some sort of BeanPostProcessor to customize it manually. – ikumen May 10 '16 at 06:46
  • 1
    I'll try that. Since your answer is not exactly what I was looking for, I'm not accepting it now but I'll upvote it. If this works out, I'll later accept your answer. – Abhijit Sarkar May 10 '16 at 07:06
  • Hi @ikumen I've tried this configuration, but my other MVC configuration is XML base, so I am getting this error: _Error creating bean with name 'resourceHandlerMapping' defined in class path resource [com/ecc/ws/rest/config/WebConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set_ – Aakash Patel Sep 20 '21 at 08:39
1

As of Spring 5.0 you can use the interface WebMvcConfigurer since Java 8 allows for default implementations on interfaces.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

   ...
   @Override
   public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
      configurer.favorParameter(..);
      ...
      configurer.defaultContentType(..);
   }
}
Björn Kahlert
  • 109
  • 1
  • 5