[英]How to send JSON request using Jersey 2.x
我正在尝试使用Jersey 2.x发送休息请求。 我可以找到的所有样本都使用Jersey1.x。
这是在Jersey 1.X中完成的方式
String jsonPayload = "{\"name\":\"" + folderName + "\",\"description\":\"" + folderDescription + "\"}";
WebResource webResource = client.resource(restRequestUrl);
ClientResponse response =
webResource.header("Authorization", "Basic " + encodedAuthString)
header("Content-Type", "application/json")
post(ClientResponse.class, jsonPayload);
在Jersey 2.x中,我该如何做?
Client client = ClientBuilder.newClient(clientConfig);
WebTarget target = client.target(m_docs_base_url + "/users/items");
String jsonPayload = "{\"info\":\"" + "smith" + "\"}";
Invocation.Builder invocationBuilder = target.request("text/plain");
Response response = invocationBuilder.get(jsonPayload);
您是否查看了Jersey客户文档?
https://jersey.java.net/documentation/latest/client.html#d0e4692
请记住,对您的JSON有效负载使用post()方法,而不是在该示例中使用get()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.