0
votes

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:

http://crazytechbuddy.blogspot.co.il/2012/06/making-jersey-to-use-jackson-instead-of.html?showComment=1360271858862#c6459334450173933715

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) ?

1

1 Answers

0
votes

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.