RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse
Asked Answered
I

8

18

I'm trying to test Keycloak REST API. Instaled the version 2.1.0.Final. I can access the admin through browser with SSL without problems.

I'm using the code above:

Keycloak keycloakClient = KeycloakBuilder.builder()
.serverUrl("https://keycloak.intra.rps.com.br/auth")
.realm("testrealm")
.username("development")
.password("development")
.clientId("admin-cli")
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
List<RealmRepresentation> rr = keycloakClient.realms().findAll();

And got the error:

javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse

javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:141)
at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:60)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy20.grantToken(Unknown Source)
at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:85)
at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:65)
at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:60)
at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:413)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:102)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy22.findAll(Unknown Source)
at br.com.rps.itsm.sd.SgpKeycloakClient.doGet(SgpKeycloakClient.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse
at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:75)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:52)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:213)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:105)

I added the dependencies above, but do not solve my problem:

    <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.19.Final</version>
    </dependency>
    <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.19.Final</version>
    </dependency>

Any clues?

Infra answered 4/10, 2016 at 21:16 Comment(3)
Did you solved ? I got same message...Beffrey
Have you solved it?Baziotes
Possible duplicate of RestEasy - Unable to find MessageBodyReader?Morph
B
9

I solved !! You must add org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider into META-INF/services/javax.ws.rs.ext.Providers file then run your executable jar.

Beffrey answered 7/11, 2016 at 10:9 Comment(4)
or org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2ProviderViv
Do you have a solution when using maven ?Tamarra
Same problem. I had already added resteasy-jackson2-provider in the pom and still had this problem.Rupee
No no no... jackson for read(but in earlier version of resteasy), jackson2 for write(now, only, I don't understand but seems it's separated now). Even though checking the hierarchy of classes, Jackson2Provider seems inherits MessageBodyReader. So strange.Chui
L
33

I used the dependency to fix this issue

   <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
Lacilacie answered 7/9, 2017 at 11:14 Comment(0)
B
9

I solved !! You must add org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider into META-INF/services/javax.ws.rs.ext.Providers file then run your executable jar.

Beffrey answered 7/11, 2016 at 10:9 Comment(4)
or org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2ProviderViv
Do you have a solution when using maven ?Tamarra
Same problem. I had already added resteasy-jackson2-provider in the pom and still had this problem.Rupee
No no no... jackson for read(but in earlier version of resteasy), jackson2 for write(now, only, I don't understand but seems it's separated now). Even though checking the hierarchy of classes, Jackson2Provider seems inherits MessageBodyReader. So strange.Chui
L
4

Or if you create client with proxy then better to initialize provider factory before:

    ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(instance);
    instance.registerProvider(ResteasyJackson2Provider.class);
Loreanloredana answered 7/2, 2018 at 7:23 Comment(0)
B
4

Adding what Dmitry pointed out here for me causes the following warning:

WARN: RESTEASY002155: Provider class org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider is already registered.  2nd registration is being ignored.

Probably as Bacon said the provider is automatically registered by simply adding the dependency:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
Beaudoin answered 16/4, 2018 at 20:23 Comment(0)
T
3

I had the problem, that MatteoM pointed out. I solved with maven-shade-plugin and the transformes tag. Maybe it helps someone.

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>de.example.MainClass</Main-Class>
                                    <X-Compile-Source-JDK>1.8</X-Compile-Source-JDK>
                                    <X-Compile-Target-JDK>1.8</X-Compile-Target-JDK>
                                </manifestEntries>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
                            </transformer>
                        </transformers>
                        <artifactSet/>
                        <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Tamarra answered 28/10, 2019 at 10:6 Comment(2)
Adding transformer for META-INF/services/javax.ws.rs.ext.Providers works.Peccadillo
There's now a ServicesResourceTransformer that takes care of all service provider files so that you don't have to specify each manually (and potentially forget some).Exoenzyme
C
2

Have you registered an instance of @Provider when creating the target/client? I see you haven't.

In my case, I was moving from Resteasy 3 to Resteasy 4, and receiving content type text/plain. In Resteasy-jaxrs 3, I used ResteasyJackson2Provider and it implements MessageBodyReader and MessageBodyWriter, and it worked. Now, in Restyeasy 4, the content type check seems to be stricter and isReadable() of this same named class only accepts Content-Type of null or contains json. As I receive text/plain, it no longer works.

For reading plain text, I suggest using StringTextStar. A new class in Resteasy 4.7.5, and it seems to work. Reading inputstream and write as string, just what I need. Check its impl.

ResteasyClient client = new ResteasyClient().uri(...)
                          .register(new ResteasyJackson2Provider()) // now only for writing
                          .register(new StringTextStar());         // use this for reading

And I think you are fine.

Chui answered 20/6, 2022 at 17:55 Comment(0)
F
1

On a wildfly 11 / Keycloak context, the following made it work with the RestEasyClientBuilder.

ResteasyClientBuilder builder = new ResteasyClientBuilder();
ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
builder.providerFactory(providerFactory);

Double check if ResteasyProviderFactory defaults to true with your Resteasy version. If not, set the builtIns manually:

ResteasyProviderFactory.setRegisterBuiltinByDefault(true);
// or if ResteasyProviderFactory was already instantiated
RegisterBuiltin.register(providerFactory);
Focus answered 15/6, 2020 at 14:47 Comment(1)
You saved my week with the first part of your answer. Thanks a ton!Lietman
P
0

In my case, I traced the root issue to the fact that I switched to CompletableFuture.

The version of the client used is probably outdated or something and while it works from eclipse, from an external executable it crashes with this error.

I'll probably post a fix, but it's going to either be: update the library or stop using the CompletableFuture on this library.

Paternoster answered 11/2, 2021 at 16:37 Comment(1)
or switch from Resteasy to something else if possiblePaternoster

© 2022 - 2024 — McMap. All rights reserved.