简体   繁体   English

使用Jersey客户端进行POST操作

[英]Using the Jersey client to do a POST operation

In a Java method, I'd like to use a Jersey client object to do a POST operation on a RESTful web service (also written using Jersey) but am not sure how to use the client to send the values that will be used as FormParam's on the server. 在Java方法中,我想使用Jersey客户端对象在RESTful Web服务上执行POST操作(也使用Jersey编写),但我不确定如何使用客户端发送将用作FormParam的值在服务器上。 I'm able to send query params just fine. 我能够发送查询参数就好了。

Not done this yet myself, but a quick bit of Google-Fu reveals a tech tip on blogs.oracle.com with examples of exactly what you ask for. 我自己还没有完成这项工作,但Google-Fu的一小部分内容在blogs.oracle.com上展示了一个技术提示,其中包含您要求的实例。

Example taken from the blog post: 从博客文章中摘取的示例:

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
ClientResponse response = webResource
    .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
    .post(ClientResponse.class, formData);

That any help? 那有什么帮助吗?

Starting from Jersey 2.x, the MultivaluedMapImpl class is replaced by MultivaluedHashMap . 从Jersey 2.x开始, MultivaluedMapImpl类被MultivaluedHashMap替换。 You can use it to add form data and send it to the server: 您可以使用它来添加表单数据并将其发送到服务器:

    WebTarget webTarget = client.target("http://www.example.com/some/resource");
    MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
    formData.add("key1", "value1");
    formData.add("key2", "value2");
    Response response = webTarget.request().post(Entity.form(formData));

Note that the form entity is sent in the format of "application/x-www-form-urlencoded" . 请注意,表单实体以"application/x-www-form-urlencoded"的格式发送。

It is now the first example in the Jersey Client documentation 它现在是Jersey客户端文档中的第一个示例

Example 5.1. 例5.1。 POST request with form parameters 带表单参数的POST请求

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:9998").path("resource");

Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");

MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
    .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        MyJAXBBean.class);

If you need to do a file upload, you'll need to use MediaType.MULTIPART_FORM_DATA_TYPE. 如果您需要进行文件上传,则需要使用MediaType.MULTIPART_FORM_DATA_TYPE。 Looks like MultivaluedMap cannot be used with that so here's a solution with FormDataMultiPart. 看起来MultivaluedMap不能与之一起使用,所以这里是FormDataMultiPart的解决方案。

InputStream stream = getClass().getClassLoader().getResourceAsStream(fileNameToUpload);

FormDataMultiPart part = new FormDataMultiPart();
part.field("String_key", "String_value");
part.field("fileToUpload", stream, MediaType.TEXT_PLAIN_TYPE);
String response = WebResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);

Simplest: 最简单的:

Form form = new Form();
form.add("id", "1");    
form.add("name", "supercobra");
ClientResponse response = webResource
  .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
  .post(ClientResponse.class, form);

Also you can try this: 你也可以尝试这个:

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
webResource.path("yourJerseysPathPost").queryParams(formData).post();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jersey客户端使用请求参数和请求正文执行POST操作 - Using the jersey client to do a POST operation with request params and a request body 如何使用球衣客户端(RestFull)读取数据以响应POST操作 - How to read data in response of POST operation using jersey client (RestFull) 如何使用Jersey 2.26客户端与queryParam进行HTTP POST请求? - How to do HTTP POST request with queryParam using Jersey 2.26 client? 使用jersey客户端编写POST请求 - Writing a POST request using a jersey client 尝试使用 Jersey 客户端 API 执行 POST 请求的“已连接”异常 - 'Already Connected' exception trying to do POST request using Jersey Client API 与Jersey客户端的POST请求 - POST requests with a Jersey client 泽西岛客户端:如何发布嵌套的JSON数据? - Jersey client: How do I POST nested JSON data? 为什么在使用Jersey客户端的POST呼叫中出现400错误代码 - Why 400 error code in POST call using Jersey client 使用APPLICATION_FORM_URLENCODED mediatype与Jersey客户端进行POST - POST with Jersey client using APPLICATION_FORM_URLENCODED mediatype 泽西岛:如何使用带有查询参数和图像的客户端发出POST请求? - Jersey: How to make a POST request using a Client with Query Parameters and an image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM