Using the Jersey client to do a POST operation

前端 未结 6 1585
暖寄归人
暖寄归人 2020-12-02 12:20

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 t

相关标签:
6条回答
  • 2020-12-02 12:30

    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.

    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?

    0 讨论(0)
  • 2020-12-02 12:33

    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:

        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".

    0 讨论(0)
  • 2020-12-02 12:34

    It is now the first example in the Jersey Client documentation

    Example 5.1. POST request with form parameters

    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);
    
    0 讨论(0)
  • 2020-12-02 12:38

    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);
    
    0 讨论(0)
  • 2020-12-02 12:50

    Also you can try this:

    MultivaluedMap formData = new MultivaluedMapImpl();
    formData.add("name1", "val1");
    formData.add("name2", "val2");
    webResource.path("yourJerseysPathPost").queryParams(formData).post();
    
    0 讨论(0)
  • 2020-12-02 12:54

    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.

    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);
    
    0 讨论(0)
提交回复
热议问题