4 Replies Latest reply on Mar 7, 2014 4:40 AM by mattias_g

    Using resteasy-jackson2-provider instead of resteasy-jackson-provider

    mattias_g

      Hello.

       

      I have am trying to use the newer com.fasterxml.jackson api instead of the old org.codehaus api without having to package jackson with the war file..

       

      I found the several discussions on the developer mailing list ([wildfly-dev] Configurable implicit module imports, [wildfly-dev] Jackson 1 and 2 coexistence ) and as I understand it I am supposed to use the jboss-deployment-structure.xml to exclude the old provider and then specify org.jboss.resteasy.resteasy-jackson2-provider as a module dependency.

       

      Right now my jboss-deployment-structure.xml looks like this.

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

      <jboss-deployment-structure>

          <deployment>

       

       

              <exclusions>

                  <module name="org.jboss.resteasy.resteasy-jackson-provider"/>

                  <module name="org.jboss.resteasy.resteasy-jettison-provider"/>

              </exclusions>

       

       

              <dependencies>

                  <module name="org.jboss.resteasy.resteasy-jackson2-provider"/>

              </dependencies>

       

       

          </deployment>

      </jboss-deployment-structure>

       

      And my code looks like:

      @ApplicationPath("/v1")

      public class App extends Application { }

       

      @Path("foo")

      public class FooController {

          @Produces("application/json")

          @GET

          public RestResponse<String> get() {

              System.out.println("HERE 1");

              return new SuccessfulRestResponse<>("Foo Bar");

          }

      }

       

       

       

      @Provider

      @Produces(MediaType.APPLICATION_JSON)

      @SuppressWarnings("unused")

      public class JacksonConfig implements ContextResolver<ObjectMapper> {

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

       

       

          @Override

          public ObjectMapper getContext(final Class<?> type) {

              final ObjectMapper mapper = new ObjectMapper();

       

       

              System.out.println("******************************************* ");

              log.info("Configured Jackson Json module");

              mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

              mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

       

       

              return mapper;

          }

      }

       

      public class RestResponse<T> {

          private final String        message;

          private final T             data;

          private final boolean       isSuccessful;

          @JsonProperty("api_status")

          private final ApiStatusCode apiStatusCode;

          public RestResponse(T data, boolean isSuccessful, final ApiStatusCode apiStatusCode, final String message) {

              this.data = data;

              this.isSuccessful = isSuccessful;

              this.apiStatusCode = apiStatusCode;

              this.message = message;

          }

           // Getters/Setters removed

       

      I also have a bean.xml in the project so the @Provider annotation should work.

       

      When I try to call /foo I get a "Could not find MessageBodyWriter for response object of type: [snip].SuccessfulRestResponse of media type: application/json", the JacksonConfig provider never gets called.

       

      If I exclude org.jboss.resteasy.resteasy-jackson2-provider and add com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider jar file to my war file then everything works as it should and JacksonConfig gets called.

       

      The org.jboss.resteasy.resteasy-jackson2-provider exists in wildflys module directory.

       

      Can anyone tell med what I am doing wrong?

       

      [Edit]

      I think I found part of the answer.

       

      Chapter 21. JSON Support via Jackson

      <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"/>

              </dependencies>

          </deployment>

      </jboss-deployment-structure>

      JacksonConfig now gets called as it should but now I get another exception:

      2014-03-05 16:45:50,763 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to foo: java.lang.NoSuchMethodError: org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider._configForWriting(Lcom/fasterxml/jackson/databind/ObjectMapper;[Ljava/lang/annotation/Annotation;)Lcom/fasterxml/jackson/jaxrs/json/JsonEndpointConfig;

        at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:137) [resteasy-jackson2-provider-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:129) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.interception.ServerWriterInterceptorContext.writeTo(ServerWriterInterceptorContext.java:62) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:118) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.security.doseta.DigitalSigningInterceptor.aroundWriteTo(DigitalSigningInterceptor.java:143) [resteasy-crypto-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:122) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:122) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:99) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:427) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.6.Final.jar:]

        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.6.Final.jar:]

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]


       

      Message was edited by: Mattias Gyllsdorff.

       

      [Edit 2]

      Resteasy Jackson 2 error in Wildfly 8 Final

      http://stackoverflow.com/questions/21780208/resteasy-and-jackson-incompatibility-nosuchmethodexception

       

      Apparently this last exceptions is a bug with jackson 2.3.0. I will either have to upgrade the provided jackson to 2.3.2 or downgrade it to 2.2.3.

       

      Message was edited by: Mattias Gyllsdorff. I am apparently not allowed to post a reply so I will have to use edit instead.

        • 1. Re: Using resteasy-jackson2-provider instead of resteasy-jackson-provider
          jamezp

          The Jackson2 dependencies were downgraded upstream to fix this issue, [WFLY-2959] Downgrade Jackson dependency - JBoss Issue Tracker. This should be fixed in 8.0.1.Final when it's released.

           

          For now you could build the upstream and use it or replace all the com.fasterxml dependencies to use the 2.2.3 version.

           

          --

          James R. Perkins

          • 2. Re: Re: Using resteasy-jackson2-provider instead of resteasy-jackson-provider
            mattias_g

            James Perkins wrote:

             

            The Jackson2 dependencies were downgraded upstream to fix this issue, [WFLY-2959] Downgrade Jackson dependency - JBoss Issue Tracker. This should be fixed in 8.0.1.Final when it's released.

             

            For now you could build the upstream and use it or replace all the com.fasterxml dependencies to use the 2.2.3 version.

             

            --

            James R. Perkins

             

            I understand. We would prefer to use jackson 2.3 due to some of the api improvements so I decided to do it like this.

             

            jboss-deployment-structure.xml:

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

            <jboss-deployment-structure>

                <deployment>

                    <exclusions>

                        <module name="org.jboss.resteasy.resteasy-jackson-provider"/>

                        <module name="org.jboss.resteasy.resteasy-jettison-provider"/>

                        <module name="org.jboss.resteasy.resteasy-jackson2-provider"/>

                    </exclusions>

                </deployment>

            </jboss-deployment-structure>

            build.gradle

            apply plugin: 'war'

            compileJava.options.encoding = 'UTF-8'

             

            sourceCompatibility = JavaVersion.VERSION_1_8

            targetCompatibility = JavaVersion.VERSION_1_8

             

            version = '1.0'


            repositories {

                mavenCentral()

            }

             

            dependencies {

                testCompile group: 'junit', name: 'junit', version: '4.11'

             

                providedCompile group: "javax", name: "javaee-api", version: "7.0"

                providedCompile 'org.jboss.logging:jboss-logging:3.1.4.GA'

             

                compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.2'

            }

             

            war {

                archiveName = "foo.war"

            }

             

            task deploy(type: Copy) {

                from war

                into "[INSERT PATH TO DEVELOPMENT WILDFLY DEPLOYMENT FOLDER]"

            }

             

            So far everything works as it should, the only downside is that the jackson dependecy adds about 1mb to the war file.

             

            Is there anything in particular that can go wrong if I do it like this? I took a look at the ResteasyJackson2Provider code and as far as I can tell we lose the NoJackson annotation and some caching(?) code.

             

            I wonder if I could use wildflys module system to let the server provided the jars and then include it using the jboss-deployment-structure.xml...


            Right now I am just evaluating wildfly and are creating a few different project blueprints for internal use so I will have to test this solution a bit more before I can say if this works or not. Is there any official basic RESTeasy hello world application template available?


            By the way, the amount of code/xml required to get something basic running have really decreased in the last couple of years. That build.gradle, the jboss-deployment-structure.xml and a empty class with the @ApplicationPath annotation is pretty much everything you need to get started, add a persistence.xml with a datasource and you pretty much have a working application.


            Now I just need to get the development exploded deployment working in Intellij IDE.

            • 3. Re: Re: Re: Using resteasy-jackson2-provider instead of resteasy-jackson-provider
              jamezp

              Is there anything in particular that can go wrong if I do it like this? I took a look at the ResteasyJackson2Provider code and as far as I can tell we lose the NoJackson annotation and some caching(?) code.

              To be honest, I'm not sure. I know version 2.3.x will not work with RESTEasy. In the previously linked JIRA there are some comments on a commit to Jackson2 that explain why 2.3.x doesn't work, Initial step for #32. Although just now realized it's wrong... will need... ·  bafb6fb · FasterXML/jackson-jaxrs-provide…. I'm not sure what the logic is, but if it does a best guess on exclusions I would guess it would use the JSON-P (JSR-353) provider. I don't actually know this, I'm just guessing

               

              For a basic Hello World example there is a quickstart. There are other quickstarts that use JAX-RS as well.

               

              --

              James R. Perkins

              1 of 1 people found this helpful
              • 4. Re: Using resteasy-jackson2-provider instead of resteasy-jackson-provider
                mattias_g

                James Perkins wrote:

                 

                Is there anything in particular that can go wrong if I do it like this? I took a look at the ResteasyJackson2Provider code and as far as I can tell we lose the NoJackson annotation and some caching(?) code.

                To be honest, I'm not sure. I know version 2.3.x will not work with RESTEasy. In the previously linked JIRA there are some comments on a commit to Jackson2 that explain why 2.3.x doesn't work, Initial step for #32. Although just now realized it's wrong... will need... ·  bafb6fb · FasterXML/jackson-jaxrs-provide…. I'm not sure what the logic is, but if it does a best guess on exclusions I would guess it would use the JSON-P (JSR-353) provider. I don't actually know this, I'm just guessing

                 

                For a basic Hello World example there is a quickstart. There are other quickstarts that use JAX-RS as well.

                 

                --

                James R. Perkins

                Ok, It looks like the bug should be fixed in jackson-jaxrs-provider 2.3.2 or the next release (Integration with RESTeasy broken with 2.3 · Issue #41 · FasterXML/jackson-jaxrs-providers · GitHub). I will just exclude RESTEasys jackson provider in the meantime.