I am using RESTEasy to implement a REST Service using JSON serialization. Currently, Dates are getting serialized to milliseconds since 1970. To improve compatibility, I would like to get my dates into one of two formats; milliseconds + timezone offset or ISO 8061.
It seems that RESTEasy used to use Jettison for JSON serialization, but from what I've been reading they've switch to Jackson ... all of this has made googling for help pretty hit or miss.
From what I can tell, I need to implement a ContextResolver along the lines of:
public class JacksonConfig impelments ContextResolver<ObjectMapper>
{
private final OBjectMapper objectMapper;
public JacksonConfig() throws Exception
{
objectMapper = new ObjectMapper.configure(
SerializationFeature.WRITE_DATE_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> arg0)
{
return objectMapper;
}
}
The thing I haven't been able to find, is what do I do with this? Where do I put it?
So the larger questions are, am I heading in the right direction and are my assumptions correct?
Just annotate your fields with (note the string literal could be externalized/referred from a constant):
Using with the JSR310 (new api date) - LocalDate, LocalDateTime, LocalTime
Add dependence:
And create a provider to register the module:
You need to register your
ContextResolver
implementation with Resteasy. You can do this by annotating your class with the@Provider
annotation and allowing Resteasy to automatically scan it during startup, registering it in web.xml, or registering it in a class that extendsjavax.ws.rs.core.Application
(if that is how you are bootstrapping Resteasy).Registering via Annotations
Verify that classpath scanning is enabled in your web.xml file like so:
NOTE: If you are deploying this in JBoss 7 do not set the
resteasy.scan
context parameter as it is enabled by default.Registering via web.xml
Add the following context parameter to your
web.xml
file. The value of the parameter should be the fully qualified class name of yourContextResolver
.Registering via Application
If you are using an Application class to configure Resteasy you can add your provider to the set of services and providers to register with Resteasy like so:
More on standalone configuration HERE