Habib
Habib

Reputation: 1

How to invoke a restlet resource

I have a restlet resource which looks like this:

@Get("json")

public List<String> retrieve() {

MyCityService nh = (MyCityService)getContext().getAttributes().get(MyCityService.class.getCanonicalName());

return nh.getReport();

}

as you can see it returns a List of Strings. And I try to get the returned value in a remote class using the following code:

ClientResource client = new ClientResource("http://remoteserver.com/mycity/nh/json");
System.out.println(client.get().getText());

The getText() method returns the whole content of the List as one string but I want to get each of the string values added in the List separately. Is there a way to do this?

Upvotes: 0

Views: 69

Answers (1)

Akshay
Akshay

Reputation: 373

I would recommend you to go for JSON Data Exchange. With minimal changes you can do it using any JSON Parser for Java library. I would recommend [JSON Lib] (http://sourceforge.net/projects/json-lib)

In your rest web service you can use

@Get("json")
@Produces("MediaType.APPLICATION_JSON") // It will return JSON Object as response
public List<String> retrieve() {

MyCityService nh = (MyCityService)getContext().getAttributes().get(MyCityService.class.getCanonicalName());

return nh.getReport();

}

And in Client part you can use JSON.parser to parse back data and into list.

   JSONObject jsonObject = (JSONObject) jsonParser.parse(client.get().getText());
   System.out.println(jsonObject.get("firstname"));
   System.out.println(jsonObject.get("firstname"));

Upvotes: 2

Related Questions