0

I'm having an unexplained behaviour of Jersey. When serialisation a date as DTO's attribute, I'll get timestamp (that's fine for me). Something like :

Java : 
Date day = new Date();

JSon :
{
  "day" :   1422831600000
}

But I having a such more complex DTO with a map using Date as key.

Java :
Map<Date, String> mapData = new HashMap<Date, String>();
     mapData.put(new Date(), "TEST");

JSON :
{
    mapData : {
        "2015-02-01T23:00:00.000+0000" : "TEST"
    }
}

How can I get simply in the map case the timestamp insteadt of ISO format ?

Thanks.

Ness
  • 1
  • 1
  • have a look at http://stackoverflow.com/questions/4428109/jersey-jackson-json-date-format-serialization-how-to-change-the-format-or-us – learningJava Feb 16 '15 at 17:22

1 Answers1

0

Finally, I've find a good way to do it. There is a config object into Jersey, that allow Jackson provider to write date as timestamp when it's used as key into a map.

@Provider
public class MyJacksonProvider implements ContextResolver<ObjectMapper>{

private ObjectMapper objectMapper;

public PolarisJacksonProvider() {
    objectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("MyModule");
    objectMapper.registerModule(module);
    objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, true);
}

/**
 * {@inheritDoc}
 * 
 * @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
 */
@Override
public ObjectMapper getContext(Class<?> type) {
    return objectMapper;
}

}

Ness
  • 1
  • 1