[英]Java REST api Send array using HTTP PUT method
I'm not able to implement an REST API in Java我无法在 Java 中实现 REST API
I've got a working example of a generic implementation using PHP.我有一个使用 PHP 的通用实现的工作示例。
How I can implement it in Java using json library and HttpClient (HttpPut request) ?我如何使用 json 库和 HttpClient(HttpPut 请求)在 Java 中实现它?
Here it is the PHP example这是 PHP 示例
//DATA TO UPDATE
$postData = array(
'item' => array(
'title' => 'My title',
'personal_reference' => 'My personal ref',
'qty' => 3,
'description' => 'My description'
)
);
//RESOURCE CALL WITH PUT METHOD
$url = 'https://rest.restserv.com/item/1234?token=MyPersonalToken';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($postData) );
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$xml_response = curl_exec($ch);
My (not working) approach was this:我的(不工作)方法是这样的:
Map<String,String> dataMap = new HashMap<String,String>();
dataMap.put("title", "some text");
dataMap.put("personal_reference", "my ref");
dataMap.put("qty", "1");
dataMap.put("description", "some desciption text");
String url = "https://rest.restserv.com/item/1234?token=MyPersonalToken";
HttpPut putRequest = new HttpPut(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
builder.addTextBody(entry.getKey(), entry.getValue());
}
putRequest.setEntity(builder.build());
response = httpClient.execute(putRequest);
thanks and best regards.谢谢和最好的问候。
UPDATE更新
Now I'm trying with this other approach.现在我正在尝试使用另一种方法。 The object Map<String,String> dataMap contains all the details of the item to send.对象 Map<String,String> dataMap 包含要发送的项目的所有详细信息。 Unfortunately i'm still not able to send item details.不幸的是,我仍然无法发送项目详细信息。 The request status is 200 and also the response is ok.请求状态为 200,响应也正常。
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPut putRequest = new HttpPut(url);
putRequest.addHeader("Content-Type", "application/json; charset=utf-8");
JSONArray itemDetails = new JSONArray();
itemDetails.put(dataMap);
JSONObject root = new JSONObject().put("item", itemDetails);
StringEntity entity = new StringEntity(root.toString(2), "UTF-8");
System.out.println("ROOT is:::: "+root.toString(2));
putRequest.setEntity(entity);
response = httpClient.execute(putRequest);
root.toString(2) result is: root.toString(2)结果是:
{"item": [{"title": "My title","personal_reference": "My personal ref","qty": "3","description": "My description"}]} {"item": [{"title": "我的标题","personal_reference": "我的个人参考","qty": "3","description": "我的描述"}]}
PROBLEM问题
It seems that payload wasn't correctly formatted and were skipped by the REST server.似乎有效载荷的格式不正确并且被 REST 服务器跳过。
SOLUTION解决方案
The problem has been solved using kong.unirest.Unirest library.该问题已使用 kong.unirest.Unirest 库解决。
Bye再见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.