java—尝试使用SpringREST客户端创建keyClope用户

nuypyhwy  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(524)

错误信息如下:
400错误请求:[未识别的字段“rep”(class org.keydrope.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=新gson();//string jsonstring=gson.tojson(用户表示);

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

cczfrluj1#

看来你应该派 HttpEntity<UserRepresentation> 作为请求。因为服务器代码等待此类型并接收“rep”:“userrepresentation”,尝试将其Map到实际的userrepresentation.class

相关问题