We had two application on spring boot. One was spring rest api based & second was spring MVC based.
We have megred both the application due to some business reasons as the context was the same and everything is working fine except java.time.LocalDateTime formatting that does by spring automatically on rest API. previously it was formatting LocalDateTime as "2018-08-30T18:13:24" but after merging it is showing as [ 2018, 08, 30, 18, 13, 24 ],
I have found out @EnableWebMVC annotation is the culprit but after removing that annotation web-mvc pages do not work.
What should I do so that date display in ISO (String) format and view resolver & jsp pages works fine?
Please help thanks.
Everyone is saying @EnableWebMvc is the culprit. But, no one is saying with WebMvc how to resolve this issue.
So, to answer the question, yes, there is a way to resolve this issue by not removing the @EnableWebMvc.
Before moving into the answer, let's understand a few concepts:
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// Remove the default MappingJackson2HttpMessageConverter
converters.removeIf(converter -> {
String converterName = converter.getClass().getSimpleName();
return converterName.equals("MappingJackson2HttpMessageConverter");
});
// Add your custom MappingJackson2HttpMessageConverter
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
converter.setObjectMapper(objectMapper);
converters.add(converter);
WebMvcConfigurer.super.extendMessageConverters(converters);
}
}
Note: Please don't use te configureMessageConverters() method instead of the extendMessageConverters() method from WebMvcConfigurer, because the configure method will remove all the existing converters are installed by default.
Hope it will help someone like me who has wasted some hours debugging the issue :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With