title: Keycloak API createUser Java
body: <p>Sample Java Code I use:</p>
  
  <pre><code>public static AjaxResponse createUser(User newUser) {
  
      Keycloak keycloak = Keycloak.getInstance(
              SERVER_URL,
              REALM,
              USERNAME,
              PASSWORD,
              CLIENT_ID);
  
      // Get Realm
      RealmResource realmResource = keycloak.realm(REALM);
      UsersResource userResource = realmResource.users();
  
      // Create User Representation
      UserRepresentation user = getUserRepresentation(newUser);
  
      // Create user (requires manage-users role)
      try {
          System.out.println("Username: {}", userResource.get("USER-ID-HERE").toRepresentation().getUsername());            
          System.out.println("Count: " + userResource.count());
          Response response = userResource.create(user);
          System.out.println("Response: " + response.getStatusInfo());
          System.out.println("Response: " + response.getStatus());
          System.out.println("Response: " + response.getMetadata());
  
      } catch (Exception e) {
          System.out.println(ExceptionUtils.getStackTrace(e));
          return new AjaxResponse("Fail", false);
      }
  
      return new AjaxResponse("Successful User Creation", true);
  }
  
  private static UserRepresentation getUserRepresentation(User newUser) {
  
      UserRepresentation user = new UserRepresentation();
      user.setEnabled(true);
      user.setUsername(newUser.getUsername());
      user.setFirstName(newUser.getFirstName());
      user.setLastName(newUser.getLastName());
      user.setEmail(newUser.getEmail());
  
      CredentialRepresentation credentialRepresentation = new CredentialRepresentation();
      credentialRepresentation.setTemporary(true);
      credentialRepresentation.setType(CredentialRepresentation.PASSWORD);
      credentialRepresentation.setValue(newUser.getUsername());
      user.setCredentials(Collections.singletonList(credentialRepresentation));
  
  }
  </code></pre>
  
  <p>The response I get when I run the code:</p>
  
  <pre><code>Username: USERNAME Correctly Identified here
  Count: 98
  Response: Conflict
  Response: 409
  Response: [Connection=keep-alive,Content-Length=46,Content-Type=application/json,Date=Tue, 03 Jul 2018 15:27:58 GMT,Server=WildFly/10,X-Powered-By=Undertow/1]`
  </code></pre>
  
  <p><strong><em>Some thoughts:</em></strong>
  I added the count so to identify if the whole connection works at all. And it seems that the count returned is correct. So I successfully connect to keycloak but something else goes wrong when I try to create a user.</p>
  
  <p>The dependencies in my pom.xml</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
          &lt;artifactId&gt;keycloak-servlet-filter-adapter&lt;/artifactId&gt;
          &lt;version&gt;3.2.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
          &lt;artifactId&gt;keycloak-admin-client&lt;/artifactId&gt;
          &lt;version&gt;3.2.1.Final&lt;/version&gt;   (Have also tried 3.1.0.Final and 3.2.0.Final)
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
          &lt;artifactId&gt;resteasy-client&lt;/artifactId&gt;
          &lt;version&gt;3.1.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
          &lt;artifactId&gt;resteasy-jackson2-provider&lt;/artifactId&gt;
          &lt;version&gt;3.1.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
          &lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt;
          &lt;version&gt;3.1.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>As I understood those dependencies are related as well:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;${jackson-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
          &lt;version&gt;${jackson-version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Other info:</p>
  
  <p>Keycloak Version I use: 3.2.0</p>
  
  <p>The Keycloak API can be found <a href="https://www.keycloak.org/docs-api/3.0/javadocs/index.html" rel="noreferrer" title="Keycloak API">here</a></p>
  
  <p>More information on the HTTP 409 Code can be found <a href="https://httpstatuses.com/409" rel="noreferrer" title="HTTP 409">here</a></p>
  
  <p>I would much appreciate any help or any guidance.
  Note Credentials and all variables have been checked, once, twice, thrice, and 10 times again. I've been searching for a complete working example. But most fail to specify the dependencies required and thus my code fails.</p>
  
  <p>Note: I am also using Tomcat-8 and as I am searching deeper into the issue. I see that there are probably some special configurations for Tomcat8. Note that the authentication sign-in sign-out for my web-app already works as expected.</p>