Question

I have a client accessing a Restful local resource. As my understanding I can specify which class do I want the respond to be. In short form I have a class Person with name and age. The webservice method SendPerson will send the a single person value ("Jon", 23). Considering I have the same Person class in the client, Is there anyway I can request the respond to send me an instance of Person class??

Something like: Person person = rest.SendPerson(Person.class); in the client (rest is my Restful Client).

Was it helpful?

Solution

I am not quite sure what you mean when you say "request the respond to send me an instance of Person class". That does not depend on the client (which you are) but on the server itself. Under ideal conditions, if the REST implementation on the server side is done correctly, the creation of the resource (here, PERSON), should normally return either the "id" of the created resource (PERSON) or the full PERSON object. The RETURN TYPE here though, depends on the server and it could be any one the MediaTypes( See here if you your server is using JAX-RS implementation). It really depends on the server implementation and not on the client. But in case, if they do return PERSON object as json (MediaType will be application/json), you can use any of the JSON libraries out there (gson is one of them) to convert this json response to a person class. Basically in your code above:

String personJson = rest.sendPerson(String.class);
Gson gson = new Gson();
Person person = gson.fromJson(personJson, Person.class);

I hope this answers your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top