dimanche 8 mars 2015

Use custom object to receive json response for Rest API



I have set up a rest API service using Jersey which produces an json response. This is the server code:



@Path("/addNumservice")
public class AddNumService {
@GET
@Produces("application/json")
public Response addNum() throws JSONException {
JSONObject jsonObject = new JSONObject();
int x = 5, y = 4;
int z = x + y;
jsonObject.put("Sum Value", z);

String result = "@Produces(\"application/json\") Output: \n\nNumber adding Output: \n\n" + jsonObject;
return Response.status(200).entity(result).build();
}
}


When I run the server, I see the o/p as expected:



@Produces("application/json") Output:
Number adding Output:
{"Sum Value":9}


Now I want to set up a client class to receive the json response in a custom object which I have defined, AddNumResponseObject, but when I do this:



AddNumResponseObject object
= webResource2.accept("application/json").get(AddNumResponseObject.class);



I get this error: A message body reader for Java class com.crunchify.client.AddNumResponseObject, and Java type class com.crunchify.client.AddNumResponseObject, and MIME media type application/json was not found



Can someone please help me?


Thanks!




Aucun commentaire:

Enregistrer un commentaire