Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@EnableWebMvc showing date in array formate

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.

like image 621
Asad Khan Avatar asked Oct 29 '25 15:10

Asad Khan


1 Answers

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:

  • HttpMessageConverters -> These are the ones that convert Java Objects from and to JSON/XML.
  • By default, Spring boot will add the following converters:
    1. ByteArrayHttpMessageConverter
    2. StringHttpMessageConverter
    3. ResourceHttpMessageConverter
    4. SourceHttpMessageConverter
    5. FormHttpMessageConverter
    6. Jaxb2RootElementHttpMessageConverter
    7. MappingJackson2XmlHttpMessageConverter
    8. MappingJackson2HttpMessageConverter
  • So, whenever we are converting the java object into JSON, Spring will go through this list of converters one by one in order and picks the relevant one to use for conversion.
  • Now, if we add our custom MappingJackson2HttpMessageConverter to this list as the last element, then Spring will not come to it because before reaching our converter(9th element), there is the default converter at the 7th index.
  • So, to resolve this issue, we need to remove the default MappingJackson2HttpMessageConverter and we need to add our custom converter.
@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 :)

like image 199
Mohan Munisifreddy Avatar answered Nov 01 '25 07:11

Mohan Munisifreddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!