简体   繁体   English

Keycloak admin-client:无法找到内容类型application / json的MessageBodyReader

[英]Keycloak admin-client: Unable to find a MessageBodyReader of content-type application/json

I'm new to keycloak and spring. 我是keycloak和spring的新手。 I'm trying to use the keycloak admin-client to create a user in my spring-boot project like this: 我正在尝试使用keycloak admin-client在我的spring-boot项目中创建一个用户,如下所示:

    Keycloak kc = Keycloak.getInstance(
    "http://localhost:8080/auth",
    "master", // the realm to log in to
    "admin", "password",  // the user
    "admin-cli");

    CredentialRepresentation credential = new CredentialRepresentation();
    credential.setType(CredentialRepresentation.PASSWORD);
    credential.setValue("test123");

    UserRepresentation user = new UserRepresentation();
    user.setUsername("testuser");
    user.setFirstName("Test");
    user.setLastName("User");
    user.setCredentials(Arrays.asList(credential));

    kc.realm("master").users().create(user);

but I keep getting this error: 但我一直收到这个错误:

 Exception in thread "main" 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:158)
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:107)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy18.grantToken(Unknown Source)
at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:89)
at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:69)
at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:64)
at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:443)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:105)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy26.create(Unknown Source)
at me.phuongtm.KeycloakAdminClientDemoApplication.main(KeycloakAdminClientDemoApplication.java:68)
  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.jaxrs.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:80)
at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:266)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:196)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:212)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:122)
... 13 more

It seems to be a problem with RESTEASY, so I added resteasy-jackson2-provider as suggested by keycloak docs, but no luck. 这似乎是RESTEASY的一个问题,所以我按照keycloak docs的建议添加了resteasy-jackson2-provider,但没有运气。

Here are my pom.xml dependecies: 这是我的pom.xml dependecies:

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-admin-client</artifactId>
        <version>3.2.0.Final</version>
    </dependency>
    <dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.1.4.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>3.1.4.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-provider</artifactId>
    <version>3.1.4.Final</version>
</dependency>
    </dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

I spent hours and hours trying to find an answer, I even tried this MatteoM's solution: 我花了几个小时试图找到答案,我甚至尝试了这个MatteoM的解决方案:

https://stackoverflow.com/a/40462534/7441720 https://stackoverflow.com/a/40462534/7441720

But no luck. 但没有运气。

I appreciate all the help I can get 我很感激能得到的所有帮助

UPDATE: 更新:

It appears to be an issue of providers: 它似乎是提供者的问题:

  10:06:45.027 [main] WARN org.jboss.resteasy.resteasy_jaxrs.i18n - 
RESTEASY002145: NoClassDefFoundError: Unable to load builtin provider 

org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider from 
 jar:file:/C:/Users/hp/.m2/repository/org/jboss/resteasy/resteasy-jackson2-
  provider/3.1.4.Final/resteasy-jackson2-provider-3.1.4.Final.jar!/META-
  INF/services/javax.ws.rs.ext.Providers

I can't seem to find a solution, I checked the jar and the class 我似乎无法找到解决方案,我检查了罐子和类

  ResteasyJackson2Provider

exists! 存在!

UPDATE: 更新:

From what I can tell, it was an issue with dependecies, I switched to gradle (but it should work for maven as well), my new dependencies are: 据我所知,这是一个依赖的问题,我切换到gradle(但它也适用于maven),我的新依赖项是:

compile('org.keycloak:keycloak-admin-client:3.2.1.Final')
compile('org.jboss.resteasy:resteasy-client:3.0.14.Final')
compile('org.jboss.resteasy:resteasy-multipart-provider:3.0.14.Final')
compile('org.jboss.resteasy:resteasy-jackson2-provider:3.0.14.Final')
compile('org.jboss.resteasy:resteasy-jaxb-provider:3.0.14.Final')
compile('org.jboss.resteasy:resteasy-jettison-provider:3.0.14.Final')
compile('org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.0_spec:1.0.0.Final')

As for keycloak, I created a Configuration class with a bean: 至于keycloak,我用bean创建了一个Configuration类:

@Bean
    public Keycloak keycloakBeanConfig(){
        return KeycloakBuilder.builder().serverUrl(keycloakUrl).realm(realm) //
                .grantType(OAuth2Constants.CLIENT_CREDENTIALS).clientId(clientId) //
                .clientSecret(clientSecret)
                .build();
    }

try to use resteasy-jackson-provider and remove resteasy-jackson2-provider . 尝试使用resteasy-jackson-provider并删除resteasy-jackson2-provider you can find dependency below: 你可以在下面找到依赖:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring 中读取 Content-Type 应用程序/json - Reading Content-Type application/json in Spring 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript JAVA-处理针对Content-Type的HttpClient请求:application / json - JAVA - Process HttpClient request for Content-Type: application/json 如何编写控制器类以允许内容类型:application / json和application / x-www-form-urlencoded - How to write controller class to allow content-type: application/json and application/x-www-form-urlencoded 当不使用 Jackson 时,Spring 将默认 Content-type 设置为“application/json;charset=utf-8” - Spring set default Content-type to “application/json;charset=utf-8” when Jackson is not used spring rest webservices使用application / json content-type返回multipart / form-data - spring rest webservices return multipart/form-data with application/json content-type 为什么POST标头Content-Type = application / json时Spring 3.1不会自动使用Jackson? - Why doesn't Spring 3.1 automatically use Jackson when POST header Content-Type=application/json? 如何让 Spring oauth2 接受 application/json 的内容类型 - How to get Spring oauth2 to accept content-type of application/json Spring mvc web 服务给 Content-Type →text/html;charset=ISO-8859-1 预期是 [{&quot;key&quot;:&quot;Content-Type&quot;,&quot;value&quot;:&quot;application/json&quot;}] - Spring mvc web service gives Content-Type →text/html;charset=ISO-8859-1 expected is [{"key":"Content-Type","value":"application/json"}] 如何添加内容类型 header application/json 以取消映射后端点? - How to add content-type header application/json to void postmapping endpoint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM