Failed adding user by keycloak-admin-client to Key

2019-07-27 13:39发布

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?

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);
}

Exception:

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)

1条回答
Viruses.
2楼-- · 2019-07-27 14:16
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.core.UriBuilder.resolveTemplates(Ljava/util/Map;)Ljavax/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"

查看更多
登录 后发表回答