1

I'm trying to map an object to JSON. This works fine, but I also want to expose the @Id in JSON. I've found this answer on how to do that, but in order to use that solution, I have to extend RepositoryRestMvcConfiguration. When I extend this, my Java 8 time formatting is breaking. My JSON was as follows:

{"name":"erik",birthDate:"2015-01-01"}

The birthDate field is a Java 8 LocalDate. Now, I also try to expose the @Id, which I do by extending RepositoryRestMvcConfiguration and setting the configuration.exposeIdsFor(MyClass.class);. Now I have the Id exposed, but, as a result of extending RepositoryRestMvcConfiguration, my LocalDate is now serialized as:

"birthDate":{"year":2015,"month":"AUGUST","chronology":{"id":"ISO","calendarType":"iso8601"},"dayOfMonth":15,"dayOfWeek":"SATURDAY","era":"CE","dayOfYear":227,"leapYear":false,"monthValue":8}

So, my question is: how can I expose the Id of my class while retaining the format of my LocalDate?

Community
  • 1
  • 1
Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • This might help you, adding the JSR module to your ObjectMapper: http://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format – jbarrueta May 14 '15 at 06:29

1 Answers1

1

It sounds like you're hitting the problem described in this Spring Boot issue. In short, the problem is that the presence of a RepositoryRestMvcConfiguration subclass is causing Spring MVC's default JSON converter to be used, rather than the one that you've configured. As described in the issue, you can work around the problem by declaring the following bean in your application's configuration:

@Bean
public HttpMessageConverters httpMessageConverters(
        final Jackson2ObjectMapperBuilder builder,
        List<HttpMessageConverter<?>> converters) {
    return new HttpMessageConverters(converters) {

        @Override
        protected List<HttpMessageConverter<?>> postProcessConverters(
                List<HttpMessageConverter<?>> converters) {
            for (HttpMessageConverter<?> converter : converters) {
                if (converter instanceof MappingJackson2HttpMessageConverter) {
                    builder.configure(((MappingJackson2HttpMessageConverter) converter)
                            .getObjectMapper());
                }
            }
            return converters;
        }
    };
}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242