I am using Jersey 1.16 for a restful service I provide to my client app.
I am trying to move from the default Jersey JSON serializer to use jackson based on this excellent blog entry:
It worked great and seems to do just what I want. however it now creates backwards compatibility issues for my restful service.
for example: timestamp fields I had that used to be serialize as "timestamp":"2012-12-25T14:22:24+02:00"
are now serialized as "timestamp":1356438144000
.
Is there a way to select to activate the new jackson based serialization mode only on specific requests (possibly use a version parameter I have to decide what type of serialization to use) ?
Jackson has lots of serialization options. In your case you need to set:
private ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
if you are using Jackson 1.x or
private ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
if you are using Jackson 2.x.
To allow Jersey to access your custom ObjectMapper
you need to create a provider:
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
private final transient ObjectMapper mapper;
@Inject
public ObjectMapperProvider()
{
this.mapper = MyMapper.getMapper();
}
@Override
public ObjectMapper getContext(final Class<?> type)
{
return this.mapper;
}
}
Where MyMapper.getMapper()
returns your custom ObjectMapper
.