KeyCloak custom REST Endpoint - Admin client classes not found

I have implemented a custom REST endpoint in KeyCloak 9.0.3. This endpoint will be invoked by an external UI layer to complete the entire registration flow including creating the user and sending the verification flow. In the SPI code, I am invoking the Admin REST client to create the user and send the verification email. I get an error when I invoke this service.

ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-42) Uncaught server error: java.lang.NoClassDefFoundError: org/keycloak/admin/client/KeycloakBuilder

My POM file has the dependency included -

<dependency>
  <groupId>org.keycloak</groupId>
  <artifactId>keycloak-admin-client</artifactId>
 </dependency>

I have tried adding a provided depenency as well but no luck.
Any ideas on what I am missing here? Or is there a problem with accessing the Admin REST API from within a custom SPI?

1 Like

It’s possible you need to add this to your jboss-deployment-structure.xml file.
An example can be found here for such a file, take note of the location of the file also.

In your case you’ll probably need to add

<module name="org.keycloak.keycloak-admin-client" />

Not tried it though but I expect this will solve your problem, let us know how it went

Thanks but now I get a …

Caused by: org.jboss.modules.ModuleNotFoundException: org.keycloak.keycloak-admin-client
at org.jboss.modules.Module.addPaths(Module.java:1266)
at org.jboss.modules.Module.link(Module.java:1622)
at org.jboss.modules.Module.relinkIfNecessary(Module.java:1650)
at org.jboss.modules.ModuleLoader.loadModule(ModuleLoader.java:299)
at org.jboss.modules.ModuleLoader.loadModule(ModuleLoader.java:283)
at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:93)

2 Likes

@sab.surendran I am getting the same error. Did you solved the issue?

I may be incorrect, but I don’t think the keycloak-admin-client is available as a module in the standard Keycloak server. You’d have to put the keycloak-admin-client jar file into your ear file at the top level, and put it in your META-INF/application.xml as a java module.

Let me know if there’s anything I can do to help with this!

Hi… That is correct. I was able to resolve it using the admin client as a module.
Add the admin client to the modules directory inside KeyCloak.
And I added a META-INF\services\jboss-deployment-structure.xml to my custom provider jar.

 <?xml version="1.0" encoding="UTF-8"?>
 <jboss-deployment-structure>
     <deployment>
         <dependencies>
             <module name="org.keycloak.keycloak-admin-client" />
         </dependencies>
     </deployment>
 </jboss-deployment-structure>

Hello,
I am stuck at the same place! Can you suggest how you added keycloak-admin-client as a module? Also for adding the said module is any jar downloaded?

Thanks,
Romil Shah

This might be a bad method. But I copied the keycloak-admin-client.xx.x.x.jar and module.xml into
…\keycloak-xx.x.x\modules\system\layers\keycloak\org\keycloak\keycloak-admin-client\main

keycloak-admin-client.xx.x.x.jar can download from https://mvnrepository.com/artifact/org.keycloak/keycloak-admin-client

and contents of the modile.xml like this,

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2016 Red Hat, Inc. and/or its affiliates
  ~ and other contributors as indicated by the @author tags.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<module name="org.keycloak.keycloak-admin-client" xmlns="urn:jboss:module:1.3">
    <resources>
        <resource-root path="keycloak-admin-client-xx.x.x.jar"/>
    </resources>

     <dependencies>
		<module name="org.keycloak.keycloak-common" />
         <module name="org.keycloak.keycloak-core"/>
		<module name="org.jboss.resteasy.resteasy-jaxrs"/>
		<module name="javax.ws.rs.api"/>
         <module name="javax.api"/>
        <module name="javax.api"/>
        <module name="javax.activation.api"/>
        <module name="sun.jdk" optional="true"/>
    </dependencies>
</module>

Then restart the keycloak server.

1 Like