[英]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.