错误信息如下:
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);
}
1条答案
按热度按时间cczfrluj1#
看来你应该派
HttpEntity<UserRepresentation>
作为请求。因为服务器代码等待此类型并接收“rep”:“userrepresentation”,尝试将其Map到实际的userrepresentation.class