Search code examples
javajbossjax-rsresteasy

RestEasyClientBuild register ResteasyJackson2Provider - WARN about duplicate


I am using the org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder to build a REST client in the following way:

ResteasyClient client = (new ResteasyClientBuilder())
    .maxPooledPerRoute(5)
    .connectionPoolSize(10)
    .socketTimeout(10L, TimeUnit.SECONDS)
    .register(jacksonProvider)
    .register(new RestClientLogger())
    .register(new RestClientMDCFilter())
    .build();

The jacksonProvider that is registered there is a ResteasyJackson2Provider jacksonProvider = new ResteasyJackson2Provider(); which comes with a custom ObjectMapper that is important for deserialization. So far so good, the problem is, that I get the following warning in JBoss:

10:31:38,414 WARN  [org.jboss.resteasy.resteasy_jaxrs.i18n] (default-threads - 1) RESTEASY002155: Provider class org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider is already registered.  2nd registration is being ignored.

This makes sense if I check the documentation when there already has been an instance of the same class that has been registered before. I used a decompiler to check what ResteasyClientBuilder is doing and was able to see, that it scans the classpath for jars that contain a resource that describes what "built in" providers should be registered.

One of the jboss provided implementations is now apparently already defining a ResteasyJackson2Provider that is registered there making it impossible for me to later register my own instance of it.

What are my options here? I need to get my version of the ResteasyJackson2Provider - or at least the ObjectMapperinto the client. Can I somehow replace the existing one?


Solution

  • I had the same issue, but I did not want to remove the resteasy-jackson2-provider dependencies because we added that dependency explicitly for other parts of the system (using undertow, not full AS).

    Turns out you can just extend the org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider without adding/overriding anything so you can register it with a higher priority than the default org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider picked up from the classpath (which is registered without a priority). This way you ensure your ResteasyJackson2Provider will be chosen over the one picked up via classpath scanning.

    Extending to a "custom" ResteasyJackson2Provider:

    public class CustomResteasyJackson2Provider extends ResteasyJackson2Provider {
    }
    

    Plug that in to the ResteasyClientBuilder with your customized ObjectMapper:

    ObjectMapper objectMapper = new ObjectMapper();
    // customize your objectMapper here...
    CustomResteasyJackson2Provider jacksonProvider = new CustomResteasyJackson2Provider();
    jacksonProvider.setMapper(objectMapper); // add objectMapper to provider
    
    // register provider to client
    ResteasyClient client = new ResteasyClientBuilder()
         .register(jacksonProvider, 100) // any priority over 0 will do
         .build();
    

    Hope this helps.