Java 正在尝试使用SpringREST客户端创建KeyClope用户
以下是错误消息: 400错误请求:[无法识别的字段“rep”(类org.keydeport.representations.idm.UserRepresentation),未标记为可忽略] 下面是一些代码:Java 正在尝试使用SpringREST客户端创建KeyClope用户,java,spring,rest,Java,Spring,Rest,以下是错误消息: 400错误请求:[无法识别的字段“rep”(类org.keydeport.representations.idm.UserRepresentation),未标记为可忽略] 下面是一些代码: public String createUser2() throws UnsupportedEncodingException, JSONException { String adminToken = getAccessToken(); System.out.pr
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();
//字符串jsonString=gson.toJson(userRepresentation)
MultiValueMap=newlinkedMultivaluemap();
map.add(“rep”,userRepresentation);
HttpEntity请求=新的HttpEntity(映射、头);
字符串uri=”http://10.127.2.46:31680/auth/admin/realms/12xDemo/users";
System.out.println(“URI:+URI”);
UserRepresentation响应=(新的RestTemplate()).postForObject(uri、请求、UserRepresentation.class);
//JSONObject obj=新的JSONObject(响应);
return(new Gson()).toJson(response);
}
看起来您应该将HttpEntity
作为请求发送。因为您的服务器代码等待此类型并接收“rep”:“UserRepresentation”,尝试将其映射到实际的UserRepresentation.class
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);
}