5 Replies Latest reply on Jan 10, 2013 11:23 AM by zlika

    Configure RESTeasy to serialize date in JSON as ISO-8601 strings

    zlika

      Hi all,

       

      I have a JAX-RS application using JBoss AS 7.1, and I POST/GET JSON and XML objects which include Dates (java.util.Date):

       

      {code}

      @XmlRootElement

      @XmlAccessorType(XmlAccessField.FIELD)

      public class MyObject implements Serializable

      {

          @XmlSchemaType(name = "dateTime")

          private Date date;

          ...

      }

      {code}

       

      When I use @Produce("application/xml") on the get method, the objets are serialized as XML and the dates are converted into ISO-8601 strings (e.g. "2012-12-10T14:50:12.123+02:00").

       

      However, if I use @Produce("application/json") on the get method, the dates in the JSON objects are timestamps (e.g. "1355147452530") instead of ISO-8601 strings.

       

      I also tried to use a custom JAX-RS provider to do the JSON serialization for Dates

       

      {code}

      @Provider

      @Produces(MediaType.APPLICATION_JSON)

      public class CustomJsonDateProvider implements MessageBodyWriter<Date>

      {

          ...

      }

      {code}

       

      This provider seems to be registered by RESTeasy on JBoss startup:

       

      {noformat}

          [org.jboss.jaxrs] Adding JAX-RS provider classes: package.CustomJsonDateProvider

          ...

          [org.jboss.resteasy.cdi.CdiInjectorFactory] No CDI beans found for class package.CustomJsonDateProvider. Using default ConstructorInjector.

      {noformat}

       

      but it is never used !

       

      How can I do to configure the JAX-RS implementation (RESTEasy) to serialize dates in JSON format as ISO-8601 strings instead of timestamps ?

       

      Thank you for your answers.

        • 1. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
          nickarls

          Not 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

          1 of 1 people found this helpful
          • 2. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
            zlika

            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

              My uneducated guess would be Jackson

              • 4. Re: Configure RESTeasy to serialize date in JSON as ISO-8601 strings
                sdnakhla

                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

                  I 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à ! :-)

                  1 of 1 people found this helpful