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

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

错误信息如下:
400错误请求:[未识别的字段“rep”(class org.keydrope.representations.idm.userrepresentation),未标记为可忽略]
下面是一些代码:

  1. public String createUser2() throws UnsupportedEncodingException, JSONException {
  2. String adminToken = getAccessToken();
  3. System.out.println("token: " + adminToken);
  4. HttpHeaders headers = new HttpHeaders();
  5. headers.set("Content-Type", "application/json");
  6. headers.set("Authorization", "Bearer " + adminToken);
  7. UserRepresentation userRepresentation = new UserRepresentation();
  8. userRepresentation.setFirstName("some");
  9. userRepresentation.setLastName("user");
  10. userRepresentation.setUsername("Some.User@somewhere.com");
  11. userRepresentation.setEmail("Some.User@somewhere.com");

//gson gson=新gson();//string jsonstring=gson.tojson(用户表示);

  1. MultiValueMap<String, UserRepresentation> map = new LinkedMultiValueMap<String, UserRepresentation>();
  2. map.add("rep", userRepresentation);
  3. HttpEntity<MultiValueMap<String, UserRepresentation>> request = new HttpEntity<>(map, headers);
  4. String uri = "http://10.127.2.46:31680/auth/admin/realms/12xDemo/users";
  5. System.out.println("URI: " + uri);
  6. UserRepresentation response = (new RestTemplate()).postForObject(uri, request, UserRepresentation.class);
  7. //JSONObject obj = new JSONObject(response);
  8. return (new Gson()).toJson(response);
  9. }
cczfrluj

cczfrluj1#

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

相关问题