Using the Jersey client to do a POST operation

前端 未结 6 1616
暖寄归人
暖寄归人 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: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 formData = new MultivaluedHashMap();
        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".

提交回复
热议问题