How to serialize JSON java.util.Date variable to ISO-8601 format?
evil850209 Apr 2, 2014 2:07 AMHi Everyone
I'm new for WildFly. Before using JavaEE 7/6, I was using Spring to development our project. Right now, in our new project, we are using JavaEE 7 platform. And we really like JBoss community, after a discuss about which server we should use, we decide to use WildFly which is easy to deployment and starting up fast.
When we development REST API for our clients. We find that, the default JSON serializer will serialize java.util.Date to a time stamp as long variable. We prefer to use ISO-8601 format for our date variable (YYYY-MM-DD hh:mm:SS T). Then I did some research on the Website, found some solutions, unfortunately, none of them works for us.
So, how to serialize JSON java.util.Date variable to ISO-8601 format? We want to make it as a default behavior without any additional annotation.
I have asked a question on the stackoverflow (http://stackoverflow.com/questions/19502878/wildfly-using-the-jackson-provider-instead-of-jettison), but up to now, no good answer for this question. I have deliver some demo code to github https://github.com/evil850209/javaee7-samples
I have read the WildFly document of how to use jackson2 provider, so I have added a jboss-deployment-structure.xml with below content to the WEB-INF folder.
<?xml version="1.0" ?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import" />
<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
</dependencies>
</deployment>
</jboss-deployment-structure>
I also update my pom file (I think 2.3.0 is the version which WildFly is using) and add a JacksonConfig class.
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
<scope>provided</scope>
</dependency>
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonConfig() {
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return objectMapper;
}
}
Now I get an error. It seem like the jackson2 provider is not load by WildFly. Is there any wrong with my jboss-deployment-structure.xml file?
Caused by: java.lang.RuntimeException: Failed to construct public org.chris.demo.rest.JacksonConfig()
at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:160)
at org.jboss.resteasy.spi.ResteasyProviderFactory.createProviderInstance(ResteasyProviderFactory.java:2175)
at org.jboss.resteasy.spi.ResteasyProviderFactory.addContextResolver(ResteasyProviderFactory.java:1072)
at org.jboss.resteasy.spi.ResteasyProviderFactory.registerProvider(ResteasyProviderFactory.java:1601)
... 16 more
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at org.chris.demo.rest.JacksonConfig.<init>(JacksonConfig.java:18)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_25]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) [rt.jar:1.7.0_25]
at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:148)