英文:
Java REST api Send array using HTTP PUT method
问题
以下是您要求的翻译内容:
我无法在Java中实现一个REST API。
我有一个使用PHP的通用实现的工作示例。
我如何使用json库和HttpClient(HttpPut请求)在Java中实现它?
以下是PHP示例代码:
//要更新的数据
$postData = array(
'item' => array(
'title' => 'My title',
'personal_reference' => 'My personal ref',
'qty' => 3,
'description' => 'My description'
)
);
//使用PUT方法的资源调用
$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);
我(不起作用的)尝试如下:
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);
感谢并致以最诚挚的问候。
更新
现在我正在尝试另一种方法。对象Map<String, String> dataMap 包含要发送的项目的所有细节。不幸的是,我仍然无法发送项目细节。请求状态为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) 的结果是:
{
"item": [
{
"title": "My title",
"personal_reference": "My personal ref",
"qty": "3",
"description": "My description"
}
]
}
英文:
I'm not able to implement an REST API in Java
I've got a working example of a generic implementation using PHP.
How I can implement it in Java using json library and HttpClient (HttpPut request) ?
Here it is the PHP example
//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. Unfortunately i'm still not able to send item details. The request status is 200 and also the response is ok.
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:
> {"item": [{"title": "My title","personal_reference": "My personal
> ref","qty": "3","description": "My description"}]}
答案1
得分: 0
问题
似乎负载的格式不正确,导致 REST 服务器跳过了它们。
解决方法
通过使用 kong.unirest.Unirest 库解决了这个问题。
再见
英文:
PROBLEM
It seems that payload wasn't correctly formatted and were skipped by the REST server.
SOLUTION
The problem has been solved using kong.unirest.Unirest library.
Bye
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论