Skip to content

Instantly share code, notes, and snippets.

@jeffbicca
Last active November 10, 2015 12:27
Show Gist options
  • Save jeffbicca/3d600b6d838f35db8a43 to your computer and use it in GitHub Desktop.
Save jeffbicca/3d600b6d838f35db8a43 to your computer and use it in GitHub Desktop.
The issue with Dates with TimeZone in JAX-RS and Jackson
package pkg;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
@Provider
@Produces("application/json")
public class JacksonConfigurator implements ContextResolver<ObjectMapper> {
public final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private ObjectMapper mapper = new ObjectMapper();
public JacksonConfigurator() {
DATE_FORMAT.setTimeZone(TimeZone.getDefault());
SerializationConfig serializationConfig = mapper.getSerializationConfig();
mapper.setSerializationConfig(serializationConfig.withDateFormat(DATE_FORMAT));
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
mapper.setDeserializationConfig(deserializationConfig.withDateFormat(DATE_FORMAT));
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> arg0) {
return mapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment