如何使用Jersey 2.x发送JSON请求

时间:2015-05-13 19:09:35

标签: java json jersey reset

我正在尝试使用Jersey 2.x发送休息请求。我能找到的所有样品都使用Jersey 1.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);

1 个答案:

答案 0 :(得分:-2)

您是否看过Jersey客户端文档?

https://jersey.java.net/documentation/latest/client.html#d0e4692

请记住在您的JSON有效负载中使用post()方法,而不是在该示例中使用get()方法。

相关问题