简体   繁体   English

Java REST api使用HTTP PUT方法发送数组

[英]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.

相关问题 使用http put第0:-1行将值从Angular 5传递到Java REST API,在输入&#39; <EOF> - Pass value from Angular 5 to Java REST API using http put, line 0:-1 no viable alternative at input '<EOF> 如何将带有JSON对象的“放置”请求发送到Java中的JSONPlaceHolder REST API - How to send a “Put” request with a JSON object to a JSONPlaceHolder REST API in Java PUT方法:无法使用Jersey使用Jersey API发送“fileName”表单数据? - PUT Method: Not able to send “fileName” form Data using Jersey API using java? 使用 java 在 Rest Assured 中发送 API 标头 - Send the API Headers in Rest Assured using java java rest API post方法提供了不允许的HTTP 405方法 - java rest API post method gives HTTP 405 method not allowed Java:发送POST HTTP请求以将文件上传到MediaFire REST API - Java: send POST HTTP request to upload a file to MediaFire REST API 使用Java调用HTTP API发送短信 - Calling HTTP API Using Java to send SMS 使用Java代码中的JIRA REST API的GET方法时出现HTTP 401异常 - HTTP 401 exception while using GET method of JIRA REST API from Java Code 在java REST API中,使用PATCH与PUT更新实体 - In a java REST API, using PATCH vs PUT to update an entity 如何使用 REST 将 JSON 数组放入 HTTP PUT 调用中? - How to put JSON array inside HTTP PUT call using REST Assured?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM