【问题标题】:Sending Name Value Pair in POST using Jersey Client使用 Jersey 客户端在 POST 中发送名称值对
【发布时间】:2012-09-21 11:33:51
【问题描述】:

如何将名称值对作为正文传递给 Jersey 的 POST ReST 服务。类似于下面使用 Apache Commons PostMethod 的代码

    final PostMethod post = new PostMethod(url);
    post.setRequestBody(new NameValuePair[] {
            new NameValuePair("loginId", userId),
            new NameValuePair("logonPassword", password),
            new NameValuePair("signature", signature),
            new NameValuePair("timestamp", timestamp),
            new NameValuePair("sourceSiteId", sourceSiteId) });

我正在将此调用移植到我的应用程序。当前调用使用 apache commons PostMethod。在我的应用程序中,我使用了泽西岛。所以我想使用球衣类/功能而不是 apache。

【问题讨论】:

    标签: java rest jersey jax-rs name-value


    【解决方案1】:

    JAX-RS 中有一个 MultivaluedMap 接口,在 Jersey 中有一个“MultivaluedMapImpl”。

    Client client = Client.create();
    WebResource webResource = client.resource("http://site.com/resource");
    MultivaluedMap<String, String> map = new MultivaluedMapImpl();
    map.put("loginId", loginId);
    ...
    ClientResponse response = webResource.type("application/x-www-form-urlencoded")
                 .post(ClientResponse.class, map);
    

    Here 是关于如何使用 Jersey 客户端 API 的更全面的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2014-02-16
      • 2013-11-10
      • 2020-11-28
      • 1970-01-01
      • 2016-01-23
      相关资源
      最近更新 更多