繁体   English   中英

尝试使用 spring rest 客户端创建 Keycloak 用户

[英]Trying to create a Keycloak user with spring rest client

这是错误消息:

400 错误请求:[无法识别的字段“rep”(类 org.keycloak.representations.idm.UserRepresentation),未标记为可忽略]

这是一些代码:

public String createUser2() throws UnsupportedEncodingException, JSONException {

    String adminToken = getAccessToken();
    
    System.out.println("token: " + adminToken);

    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "application/json");
    headers.set("Authorization", "Bearer " + adminToken);
    
    UserRepresentation userRepresentation = new UserRepresentation();
    userRepresentation.setFirstName("some");
    userRepresentation.setLastName("user");
    userRepresentation.setUsername("Some.User@somewhere.com");
    userRepresentation.setEmail("Some.User@somewhere.com");
    

// Gson gson = new Gson(); // String jsonString = gson.toJson(userRepresentation);

    MultiValueMap<String, UserRepresentation> map = new LinkedMultiValueMap<String, UserRepresentation>();
    map.add("rep", userRepresentation);
    
    HttpEntity<MultiValueMap<String, UserRepresentation>> request = new HttpEntity<>(map, headers);
    
    String uri = "http://10.127.2.46:31680/auth/admin/realms/12xDemo/users";
    System.out.println("URI: " + uri);
    
    UserRepresentation response = (new RestTemplate()).postForObject(uri, request, UserRepresentation.class);
    //JSONObject obj = new JSONObject(response);

    return (new Gson()).toJson(response);
}

看起来您应该发送HttpEntity<UserRepresentation>作为请求。 导致您的服务器代码等待此类型并接收“rep”:“UserRepresentation”,尝试在实际 UserRepresentation.class 上执行 map

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM