【问题标题】:How to send JSON request using Jersey 2.x如何使用 Jersey 2.x 发送 JSON 请求
【发布时间】:2015-07-25 05:51:23
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: java json jersey reset


    【解决方案1】:

    您查看泽西岛客户端文档了吗?

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多