Sunday, March 9, 2014

RESTEasy format timestamps as date

By default RESTEasy using Jackson will format a java.util.Date as a unix-timestamp. To change it into ISO_8601 formar, yyyy-MM-dd'T'hh:mm:ss'Z', simply create a class
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

/**
 * This class will make sure JSON timestamps is written in the format yyyy-MM-dd'T'hh:mm:ss'Z' 
 */

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {

 private final ObjectMapper objectMapper;

 public JacksonConfig() {
  objectMapper = new ObjectMapper();
  objectMapper.configure(
    SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
 }

 @Override
 public ObjectMapper getContext(Class<?> objectType) {
  return objectMapper;
 }
}

No comments:

Post a Comment