I'm using a mix of xml and codebased configuration. One part of my code configuration was ignored. This one:
@Configuration
@EnableWebMvc
public class RestMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
super.configureMessageConverters(converters);
}
@Bean
public MappingJackson2HttpMessageConverter converter() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(mapper);
return converter;
}
}
I debug the configureMessageConverters()
method on startup of the Spring MVC app, and it is executed. Still, my JSON responses in the controllers are not converting Joda LocalDate into a String representation, but gives back the whole object structure.
Why is it ignored?
Using a combination of xml and code based configuration, it's easy to end up in a situation where you configure one thing twice, making Spring overwriting previous configurations. In my case, this was due to MVC also being configured in the xml:
<context:component-scan base-package="uk.co.imperatives.billing.rest" />
<mvc:annotation-driven />
<context:annotation-config />
So, in this case, the xml configuration resetted my code configuration. Removing the <mvc:annotation-driven />
fixed the problem. So, this worked:
<context:component-scan base-package="uk.co.imperatives.billing.rest" />
<context:annotation-config />