-
1. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
nickarls Dec 12, 2012 7:46 AM (in response to zlika)1 of 1 people found this helpfulNot really an AS question but have you tried something like
@Provider @Produces(MediaType.APPLICATION_JSON) public class JacksonConfig implements ContextResolver<ObjectMapper> { private ObjectMapper objectMapper; public JacksonConfig() throws Exception { this.objectMapper = new ObjectMapper(); this.objectMapper.setDateFormat(new SimpleDateFormat("dd.MM.yyyy")); this.objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); } public ObjectMapper getContext(Class<?> objectType) { return objectMapper; } }
public class DateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date)); } }
(with Jackson, that is). Not sure if both classes are required...
The links in http://stackoverflow.com/questions/11230954/jax-rs-jackson-json-provider-date-format-issue might also be useful
-
2. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
zlika Dec 12, 2012 9:39 AM (in response to nickarls)Thank you for your answer.
In my Maven pom.xml, I only use the jboss-jaxrs-api_1.1_spec artifact, but I do not specify any explicit JSON library: do you know if, in this case, the default JSON provider is Jettison or Jackson ?
-
3. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
nickarls Dec 12, 2012 11:30 AM (in response to zlika)My uneducated guess would be Jackson
-
4. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
sdnakhla Jan 9, 2013 2:27 PM (in response to zlika)I've been having the exact same problem, but haven't had a chance to test out Nicklas' suggestion. Were you able to get it working via this suggestion?
-
5. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
zlika Jan 10, 2013 11:23 AM (in response to sdnakhla)1 of 1 people found this helpfulI solved my problem:
1- I explicitly added resteasy-jackson-provider and jackson-jaxrs dependencies in my pom.xml to activate Jackson as the JSON provider
2- I created the following class to configure date serialization with Jackson:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper>
{
private final ObjectMapper;
public JacksonConfig()
{
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> objectType)
{
return objectMapper;
}
}
et voilà ! :-)