使用Jersey客户端在POST中发送名称值对

bas*_*mes 3 java rest jax-rs jersey name-value

如何将名称值对作为正文传递给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) });
Run Code Online (Sandbox Code Playgroud)

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

dce*_*chi 12

在Jersey 中有一个带有'MultivaluedMapImpl' 的MultivaluedMap接口JAX-RS.

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);
Run Code Online (Sandbox Code Playgroud)

以下是如何使用Jersey客户端API的更全面的示例.