2 Replies Latest reply on Apr 2, 2014 11:48 PM by Chris Li

    How to serialize JSON java.util.Date variable to ISO-8601 format?

    Chris Li Newbie

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

        • 1. Re: How to serialize JSON java.util.Date variable to ISO-8601 format?
          James Perkins Master

          There is an issue using Jackson 2.3.2 in WildFly with RESTEasy. This is fixed with a new release of RESTEasy or by downgrading the Jackson module to 2.2.3. This will be fixed in WildFly 8.0.1.Final BTW.

           

          --

          James R. Perkins

          1 of 1 people found this helpful
          • 2. Re: How to serialize JSON java.util.Date variable to ISO-8601 format?
            Chris Li Newbie

            Thank for James' answer.

             

            Now, I am using the default Jackson provider (org.jboss.resteasy.resteasy-jackson-provider), and change the Jackson config code as below:

             

            import javax.ws.rs.Produces;

            import javax.ws.rs.core.MediaType;

            import javax.ws.rs.ext.ContextResolver;

            import javax.ws.rs.ext.Provider;

             

            import org.codehaus.jackson.map.ObjectMapper;

            import org.codehaus.jackson.map.SerializationConfig;

             

            @Provider

            @Produces(MediaType.APPLICATION_JSON)

            public class JacksonConfig implements ContextResolver<ObjectMapper> {

                private final ObjectMapper objectMapper;

                public JacksonConfig() {

                    objectMapper = new ObjectMapper();

                    objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

                }

                @Override

                public ObjectMapper getContext(Class<?> type) {

                    return objectMapper;

                }

            }