【问题标题】:How to pass value using httpput method in jackson?如何在杰克逊中使用httpput方法传递值?
【发布时间】:2016-10-19 09:21:41
【问题描述】:

我正在使用核心 java 和 jackson 来使用 REST 服务。现在我需要使用 HttpPut 方法进行更新。如何使用此方法更新值 我的请求数据是这样的。

{
  "comments": "Notes",
  "displaynumber": "AA245",
  "order": 1
}

我正在使用这种类型的代码:

HttpPut httpput = new HttpPut(targetURI + "comments/1001");
httpput.setConfig(config);
httpput.addHeader("content-Type","application/json");
httpput.addHeader("Accept", "application/json");

在这段代码之后如何使用jackson发送那个Json数据?而且我也在这个网址上使用基本身份验证。

【问题讨论】:

  • 如果您已经在 java 字符串变量中拥有 JSON 数据,则不需要使用 jackson。这是使用 HttpPost 发送 JSON 的示例。使用HttpPut应该差不多了:gnovus.com/blog/programming/…

标签: java jackson basic-authentication restful-authentication


【解决方案1】:

在这里我可以解决这个问题的答案。

 httpput.setEntity(new StringEntity(
    "{\"comments\":\"" + comm + "\",\"displaynumber\":\"" + tcn
      + "\",\"order\":\"" + orderAdd + "\"}",
        ContentType.create("application/json")));

 response = httpClient.execute(target, httpput);

对于jackson,我们可以使用 ObjectMapper.writeValue();

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多