Java 使用 Jersey 客户端进行 POST 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2136119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the Jersey client to do a POST operation
提问by Jon Onstott
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. I'm able to send query params just fine.
在 Java 方法中,我想使用 Jersey 客户端对象对 RESTful Web 服务(也使用 Jersey 编写)执行 POST 操作,但不确定如何使用客户端发送将用作 FormParam 的值在服务器上。我可以很好地发送查询参数。
采纳答案by brabster
Not done this yet myself, but a quick bit of Google-Fu reveals a tech tip on blogs.oracle.comwith 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?
那有什么帮助吗?
回答by RVelarde
Also you can try this:
你也可以试试这个:
MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
webResource.path("yourJerseysPathPost").queryParams(formData).post();
回答by dimuthu
If you need to do a file upload, you'll need to use MediaType.MULTIPART_FORM_DATA_TYPE. Looks like MultivaluedMap cannot be used with that so here's a solution with FormDataMultiPart.
如果您需要上传文件,则需要使用 MediaType.MULTIPART_FORM_DATA_TYPE。看起来 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);
回答by tonga
Starting from Jersey 2.x, the MultivaluedMapImpl
class is replaced by MultivaluedHashMap
. You can use it to add form data and send it to the server:
从 Jersey 2.x 开始,MultivaluedMapImpl
该类被替换为MultivaluedHashMap
. 您可以使用它来添加表单数据并将其发送到服务器:
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"
.
回答by supercobra
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);
回答by otonglet
It is now the first example in the Jersey Client documentation
它现在是Jersey 客户端文档中的第一个示例
Example 5.1. POST request with form parameters
例 5.1。带有表单参数的 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);