RestEasyClientBuild register ResteasyJackson2Provider - Duplication WARN
I am using org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
to create a REST client like this:
ResteasyClient client = (new ResteasyClientBuilder())
.maxPooledPerRoute(5)
.connectionPoolSize(10)
.socketTimeout(10L, TimeUnit.SECONDS)
.register(jacksonProvider)
.register(new RestClientLogger())
.register(new RestClientMDCFilter())
.build();
Registered jacksonProvider
is ResteasyJackson2Provider jacksonProvider = new ResteasyJackson2Provider();
, which comes with customizable ObjectMapper
, which is important for deserialization. So far so good, the problem is that I got 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 was already an instance of the same class that was registered earlier. I used a decompiler to test what it does ResteasyClientBuilder
, and he could see that it was looking at the classpath for the jars containing the resource describing which "built-in" providers should be registered.
One of the implementations provided by jboss now apparently already defines one registered there ResteasyJackson2Provider
, making it impossible to later register my own instance.
What are my options here? I need to get my version, ResteasyJackson2Provider
or at least ObjectMapper
in the client. Can I somehow replace the existing one?
source to share
I had the same problem, but I didn't want to remove the dependencies of resteasy-jackson2-provider, because we explicitly added this dependency for other parts of the system (using basow, not the full AS).
It turns out that you can just extend org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
without adding / overriding anything so that you can register it with a higher priority than the org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
one selected from the classpath (which is registered with no priority). This way you ensure that your jackson2provider is selected over the one that was selected through the class scan.
Extending to "custom" jackson2provider:
public class CustomResteasyJackson2Provider extends ResteasyJackson2Provider {
}
Connect this to the ResteasyClientBuilder using a custom 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 it helps.
source to share
If you want to use your own "resteasy-jackson2-provider" packaged jars, then one way you can skip loading the resteasy provider is provided by my JBoss, excluding the below dependencies using 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>
source to share