how to send json object from REST client using javax.ws.rs.client.WebTarget

后端 未结 1 966
庸人自扰
庸人自扰 2020-12-05 23:26

I have a POJO given below which I want to PUT to the server as JSON or XML.

This is what I have done

CLIENT:

ClientConfig co         


        
相关标签:
1条回答
  • 2020-12-06 00:11

    There are two different Jersey major versions, 1.x and 2.x, You seems to be trying to use a combination of both, which won't work. The 2.x versions don't have some classes as in 1.x and vice versa.

    If you want to use Jersey 2.x, then you should be using Response, rather than ClientResponse

    Response response = target.request().put(Entity.json(friend));
                                            // .json == automatic 'application/json'
    
    • See Working with the Client API for 2.x
    • Also as mentioned in your previous post, the getters and setters should be public for the Friend class
    • Also see the WebTarget API

    Basic breakdown.

    • Calling request() on WebTarget returns an Invocation.Buidler

      Invocation.Builder builder = target.request();
      
    • Once we call put, we get back a Response

      Response response = builder.put(Entity.json(friend));
      
    • To extract a known type from the response, we could use readEntity(Class type)

      String responseString = response.readEntity(String.class);
      response.close();
      
    0 讨论(0)
提交回复
热议问题