问题描述
我尝试在如下所示的Java代码中使用keycloak-admin-client,然后出现异常.从例外情况来看,即使在到达Keycloak之前,它似乎也未能构造API URL.有人知道为什么会这样吗?
I tried to use keycloak-admin-client in Java code like below, and then I got exceptions. From the exceptions, it looks like that it failed in constructing the API URL, even before reaching Keycloak. Does anybody have idea why this happened?
我正在docker容器中运行Keycloak并暴露了端口8078.
I am running Keycloak inside a docker container and exposed port 8078.
public static void main(String ... args) {
Keycloak kc = KeycloakBuilder.builder()
.serverUrl("http://localhost:8078/auth")
.realm("master")
.username("admin")
.password("admin")
.clientId("admin-cli")
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
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.setEnabled(true);
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);
}
例外:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.core.UriBuilder.resolveTemplates(Ljava/util/Map;)Ljavax/ws/rs/core/UriBuilder;
at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.resolveTemplates(ClientWebTarget.java:158)
at org.jboss.resteasy.client.jaxrs.internal.proxy.SubResourceInvoker.invoke(SubResourceInvoker.java:65)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy39.realm(Unknown Source)
at org.keycloak.admin.client.Keycloak.realm(Keycloak.java:71)
at com.gallagher.security.keycloak.client.Program.main(Program.java:76)
推荐答案
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.core.UriBuilder.resolveTemplates(Ljava/util/Map;)Ljavax/ws/rs/core/UriBuilder;
此错误通常表明您在路径上有2个版本的类.
如果您使用像Maven这样的依赖框架,则可以验证继承关系中是否包含正确的库
此类-"javax.ws.rs.core.UriBuilder"
This error usually indicates you have 2 versions of the class on the path.
If you use a dependency framework like Maven, you can verify the correct libraries are included in the heirarchy
This class - "javax.ws.rs.core.UriBuilder"
这篇关于由于“未知资源",无法通过keycloak-admin-client将用户添加到Keycloak.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!