3 Replies Latest reply on Apr 4, 2012 2:47 PM by Harald Pehl

    Resteasy jboss7 jackson configuration

    Ryan Connolly Newbie

      Hello folks,

       

      I am hoping someone might be able to tell me how to enable a single Jackson feature for JAX-RS JSON serialization.  I am running JBoss AS 7 and am using the default Resteasy/Jackson implementations.  All I want to do is to enable a JsonGenerator.Feature.  Anyone know how to do this?

       

      Thanks a lot in advance,

      -Ryan

        • 1. Re: Resteasy jboss7 jackson configuration
          Thomas Letsch Newbie

          A late answer, but perhaps it solves someone else's day:

           

          I have a customer jaxrs provider for my configuration:

           

          {code}

          import java.text.SimpleDateFormat;

          import java.util.logging.Logger;

           

          import javax.ws.rs.Consumes;

          import javax.ws.rs.Produces;

          import javax.ws.rs.core.MediaType;

          import javax.ws.rs.ext.Provider;

           

          import org.codehaus.jackson.map.DeserializationConfig;

          import org.codehaus.jackson.map.ObjectMapper;

          import org.codehaus.jackson.map.SerializationConfig;

          import org.codehaus.jackson.map.annotate.JsonSerialize;

          import org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider;

           

          @Provider

          @Consumes({ MediaType.APPLICATION_JSON, "text/json" })

          @Produces({ MediaType.APPLICATION_JSON, "text/json" })

          public class JacksonConfigurator extends ResteasyJacksonProvider {

           

              private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ";

           

              private static final Logger log = Logger.getLogger(JacksonConfigurator.class.getName());

           

              public JacksonConfigurator() {

                  super();

                  log.info("configuring date handling");

                  ObjectMapper mapper = _mapperConfig.getConfiguredMapper();

                  configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

                  configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

                  SerializationConfig serConfig = mapper.getSerializationConfig();

                  serConfig.setDateFormat(new SimpleDateFormat(DATE_FORMAT));

           

                  serConfig.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

                  DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();

                  deserializationConfig.setDateFormat(new SimpleDateFormat(DATE_FORMAT));

           

              }

           

          }{code}

           

          Additionally I had to enable the folowing JBoss module dependencies in my war file: org.jboss.resteasy.resteasy-jackson-provider,org.codehaus.jackson.jackson-jaxrs,org.codehaus.jackson.jackson-mapper-asl,org.codehaus.jackson.jackson-core-asl

          That was all. On the first rest request, the mapper gets configured.

          • 2. Re: Resteasy jboss7 jackson configuration
            Thomas Letsch Newbie

            Note:

            If I enable CDI (weld), my custom Jackson Provider won't get used anymore. Without weld this works fine.

             

            There is already filed a bug: https://issues.jboss.org/browse/RESTEASY-555

             

            Thanks,

            Thomas

            • 3. Re: Resteasy jboss7 jackson configuration
              Harald Pehl Newbie

              I found a way to use both CDI and a custom jackson configuration. The trick is to exclude the configuration from weld in beans.xml:

               

              <?xml version="1.0" encoding="UTF-8"?>

              <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                        xmlns:weld="http://jboss.org/schema/weld/beans"

                        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd

                  http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">

                <weld:scan>

                  <weld:exclude name="your.custom.JacksonConfigurator" />

                </weld:scan>

              </beans>

               

              In case your jackson configuration is not recognized, add the following context parameter to your web.xml

               

              <context-param>

                <param-name>resteasy.providers</param-name>

                <param-value>your.custom.JacksonConfigurator</param-value>

              </context-param>