Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I POST a Pojo with Jersey Client without manually convert to JSON?

I have a working json service which looks like this:

@POST
@Path("/{id}/query")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(JSON)
public ListWrapper query(@Context SecurityContext sc, @PathParam("id") Integer projectId, Query searchQuery) {
    ...
    return result
}

The query object looks like this and when posting a json representation of that Query object it works out nice.

@XmlRootElement
public class Query {
    Integer id;
    String query;
    ... // Getters and Setters etc..
}

Now I want to fill that object from a client and use Jersey client to post that Query object to the service and get an JSONObject as a result. My understanding is that it could be done without converting it to a json object first and then posted as a String.

I have tried something like this but I think I miss something.

public static JSONObject query(Query searchQuery){
    String url = baseUrl + "project/"+searchQuery.getProjectId() +"/query";
    WebResource webResource = client.resource(url);
    webResource.entity(searchQuery, MediaType.APPLICATION_JSON_TYPE);
    JSONObject response = webResource.post(JSONObject.class);
    return response;
}

I'm using Jersey 1.12.

Any help or pointer in the right direction would be much appreciated.

like image 342
Perty Avatar asked Apr 26 '12 14:04

Perty


People also ask

What type of JSON support is available in Jersey?

Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T> providers distributed with jersey-json module. These providers enable using three basic approaches when working with JSON format: POJO support. JAXB based JSON support.

How to create POJO class in Rest Assured?

To create a POJO class of the JSON request body, Right-click on the above-created request Package and select New >> Class.

How to convert JSON request to Java Object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.


2 Answers

The WebResource.entity(...) method doesn't alter your webResource instance... it creates and returns a Builder object that holds the change. Your call to .post is typically performed from a Builder object rather than from the WebResource object. That transition is easily obscured when all the requests are chained together.

public void sendExample(Example example) {
    WebResource webResource = this.client.resource(this.url);
    Builder builder = webResource.type(MediaType.APPLICATION_JSON);
    builder.accept(MediaType.APPLICATION_JSON);
    builder.post(Example.class, example);
    return;
}

Here's the same example using chaining. It's still using a Builder, but less obviously.

public void sendExample(Example example) {
    WebResource webResource = this.client.resource(this.url);
    webResource.type(MediaType.APPLICATION_JSON)
      .accept(MediaType.APPLICATION_JSON)
      .post(Example.class, example);
    return;
}
like image 138
phatfingers Avatar answered Oct 09 '22 14:10

phatfingers


If your web-service produces a JSON you must handle that in your client by using an accept() method:

ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).post(searchQuery, MediaType.APPLICATION_JSON);
ListWrapper listWrapper = response.getEntity(ListWrapper.class);

Try this and give your results.

like image 24
Alex Stybaev Avatar answered Oct 09 '22 13:10

Alex Stybaev