04/02/2013

Spring + Resteasy + WRITE_DATES_AS_TIMESTAMPS


If you want to configure the ObjectMapper to serialize date as string well here it is.

First you create your custom implementation of ContextResolver<ObjectMapper> :

@Provider
public class MyJacksonConfig  implements ContextResolver<ObjectMapper> {
 
  @Override
  public ObjectMapper getContext(Class type) {
   ObjectMapper mapper = new ObjectMapper();
   mapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);   
   mapper.setAnnotationIntrospector(
new Pair(
new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector()));
   return mapper;
  }
 
}

then you just need to add the provider to the resteasy.providers list in the web.xml :

 <context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider,org.rest.MyJacksonConfig</param-value>
    </context-param>

and voila', your DateTime will be serialized as ISO string format (eg: Created:2013-02 04T21:07:00.688+01:00)

Hope this will save some of your time guys :)

No comments:

Post a Comment