Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试使用SpringREST客户端创建KeyClope用户_Java_Spring_Rest - Fatal编程技术网

Java 正在尝试使用SpringREST客户端创建KeyClope用户

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

以下是错误消息:

400错误请求:[无法识别的字段“rep”(类org.keydeport.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(); //字符串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);
}