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 config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());

public void putFriend(String uri , Friend friend)
{
    System.out.println(friend.toString());

    target = target.path(some_path).path(uri);
    ClientResponse response =        target.request(MediaType.APPLICATION_JSON).put(Entity.entity(friend,MediaType.APPLICATION_JSON),ClientResponse.class);
}

Examples I found on web were using WebResource.

I don't know how to do using WebTarget. What I have done is taken from some example found on SO but Entity.entity() gives error undefined method entity(friend, String).

POJO

@XmlRootElement
public class Friend{

    private String friendURI;
    private String event;
    private String uri;

    String getUri() {
        return uri;
    }
    void setUri(String uri) {
        this.uri = uri;
    }
    String getFriendURI() {
        return friendURI;
    }
    void setFriendURI(String friendURI) {
        this.friendURI = friendURI;
    }
    String getEvent() {
        return event;
    }
    void setEvent(String event) {
        this.event = event;
    }
public String toString() {
        return "Friend [friendURI=" + friendURI + ", uri=" + uri + ", event=" + event
                 + "]";
}

Please guide how to do this.

Thanks

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
user3275095
  • 1,425
  • 4
  • 20
  • 32

1 Answers1

48

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'

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();
    
Community
  • 1
  • 1
Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
  • Thanks a lot for helping me every time. Entity.json(friend) gives error that method not define json(friend). – user3275095 Nov 30 '14 at 07:56
  • 2
    Make sure `Entity` is [`javax.ws.rs.client.Entity`](https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Entity.html) – Paul Samsotha Nov 30 '14 at 07:58
  • Oh sorry, wrong Entity class was imported. Corrected that. Thanks. And yes getters and setters are public. Just copied the code from older post. – user3275095 Nov 30 '14 at 08:01
  • One more thing, you explain really well. Thumbs up for that. Do you have any blog or website? It would be really helpful for newbies. Otherwise I guess I know who to meet :). – user3275095 Nov 30 '14 at 08:06
  • Thank you very much peeskillet. Could you also show the example where you could use the access token as header in the request after getting it from the Oauth2 process and do the post request for login? – seleniumlover Apr 21 '16 at 13:30
  • For simple, key value pair, we can use Map and use Entity.json(mapObj). – Sabarish Dec 06 '16 at 19:51
  • I have a API which is developed in spring boot , I am trying to hit that API, and I have passed my POJO object in the Entity.json , but it is not getting mapped with Object there in the API. I am getting 400 from my API , which I send when the parameters are not present. What might be the issue. – Salman Shaikh Aug 04 '17 at 13:01
  • @SalmanShaikh Please post another question on SO. I cannot tell without seeing some code. – Paul Samsotha Aug 04 '17 at 13:02
  • `String responseString = response.readEntity(String.class); response.close();` : this line save me – Atul Kumar Jun 28 '19 at 20:29