Just curious if i am missing something in this article or is providing a WebMvcConfigurationSupport falling under what is decribed "Without Spring Boot".
I am providing a custom Jackson2ObjectMapperBuilder via an imported configuration class but it is not used by the MappingJackson2HttpMessageConverter. I am not using @EnableMvc because i have to customize the mvc configuration by extending the WebMvcConfigurationSupport which internally creates the MessageConverter without requesting my jacksonBuilder bean :
if (jackson2Present) {
messageConverters.add(new MappingJackson2HttpMessageConverter());
}
Here is the WebConfig which gets imported:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
...
return factory.createMultipartConfig();
}
}
And here the configuration object which provides a custom jackson builder:
@Configuration
public class JsonConfig {
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
return new Jackson2ObjectMapperBuilder()
.indentOutput(true)
.serializers(...)
;
}
}
ping @sdeleuze
Or is this the preferred way:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Autowired
ObjectMapper objectMapper;
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
...
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
Indeed, WebMvcConfigurationSupport (as well as @EnableWebMvc + WebMvcConfigurerAdapter) is part of Spring MVC configuration mechanism, and using it unplug Spring Boot Web auto-configuration, including the capability to use a Jackson2ObjectMapperBuilder bean to configure Jackson message converters.
Please notice than MappingJackson2HttpMessageConverter constructor use Jackson2ObjectMapperBuilder as you can see here, so Jackson builder will be used but not the customized one.
My first question would be: are you sure that you need to extend WebMvcConfigurationSupport ? If you can just use the Spring Boot way (@EnableAutoConfiguration + declaring beans in @Configuration annotated classes), this is the preferred way.
If you really need to customize WebMvcConfigurationSupport, creating an ObjectMapper bean created by the builder or a Jackson2ObjectMapperBuilder one (if you need to configure both JSON and XML Jackson message converter) and using this bean to create Jackson message converter (like in your last comment) is possible. You can also just do what I proposed in the "Without Spring Boot" section of my article.
I think i have to customize WebMvcConfigurationSupport to allow the modification of MultipartConfigElement by overriding multipartConfigElement().
Are you sure about that ?
WebMvcConfigurationSupport has no multipartConfigElement() method, and seems to be just a regular @Bean declaration that could be done in a @Configuration annotated class that does not extend WebMvcConfigurationSupport.
Sorry, my fault. Its just a bean declaration. This could be a way around the issue. Thank you for your time.
Maybe WebMvcConfigurationSupport could check if theres an object mapper present but this would be more of a spring mvc issue.
@sdeleuze I would like to configure ISO date Strings with Joda globally. According to the docs (framework reference chapter 7.7) WebMvcConfigurationSupport must be extended to achieve this.
Is it really necessary to turn off all the nice Spring Boot configuration logic just to configure the date format?
Looking at the bottom of WebMvcAutoConfiguration.java it seems to me that the EnableWebMvcConfiguration class should allow a customization of the mvcConversionService.
This service uses the default constructor of DefaultFormattingConversionService which in turn registers the Joda Date formatters without any possibility to customize the format.
@owahlen Could you try to see if using Jackson2ObjectMapperBuilder#dateFormat() or Jackson2ObjectMapperBuilder#simpleDateFormat() to configure a Jackson2ObjectMapperBuilder bean like in this code sample fit your need ?
@sdeleuze unfortunately this does not really help: I would like to have iso format when binding to DateTime in command objects AND when rendering DTOs using Jackson. To use your example (and achieve the later) there must not exist an instance of WebMvcConfigurationSupport. In order to achieve the first there must be a WebMvcConfigurationSupport instance according to the docs.
This is kind of a contradiction.
My proposal would be to override mvcConversionService in EnableWebMvcConfiguration and allow custom implementations of this bean. Then do something like this: http://docs.spring.io/spring/docs/4.1.4.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#format-configuring-formatting-globaldatetimeformat
What do you think?
@owahlen OK, thanks for the additional details. Please create a new Spring Boot issue for this one, or even better, feel free to contribute a Pull Request.
@owahlen I've raised #2216 for this. Feel free to track that issue.
how can we using these Jackson2ObjectMapperBuilder in case of ENUM objects ??
@JagmohanSharma Sorry, I don't understand your question and a closed issue isn't really the best place to ask anyway. Stack Overflow (using the spring-boot tag) or Gitter are better places to get some help.