Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 633
user3275095 Avatar asked Nov 30 '14 07:11

user3275095


People also ask

What is WebTarget Java?

WebTarget matrixParam(String name, Object... values) Create a new WebTarget instance by appending a matrix parameter to the existing set of matrix parameters of the current final segment of the URI of the current target instance. If multiple values are supplied the parameter will be added once per value.

What is Jerseyclient?

Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2. For a discussion on the creation of RESTful Web Services using Jersey, please refer to this article.

What is Entity JSON?

Simple (scalar) properties of an entity/object are represented in as name/value pairs in the JSON object. The JSON name contains the property name, and value contains a JSON value that can be either a JSON string, number, boolean or null, depending on the property type.

What is JAX-RS Client API?

The JAX-RS client API is a Java based API used to access Web resources.


1 Answers

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();
    
like image 61
Paul Samsotha Avatar answered Oct 09 '22 07:10

Paul Samsotha