如何调用restlet资源



我有一个restlet资源,它看起来像这样:

@Get("json")
public List<String> retrieve() {
MyCityService nh = (MyCityService)getContext().getAttributes().get(MyCityService.class.getCanonicalName());
return nh.getReport();
}
如你所见,

返回string列表。我尝试使用以下代码在远程类中获取返回值:

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

getText()方法将List的整个内容作为一个字符串返回,但我想分别获得List中添加的每个字符串值。有办法做到这一点吗?

我建议您使用JSON Data Exchange。您可以使用任何Java库的JSON解析器进行最小的更改。我推荐[JSON Lib] (http://sourceforge.net/projects/json-lib)

在你的rest web service中,你可以使用

@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();
}

在客户端部分你可以使用JSON。解析器解析回数据和到列表。

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

最新更新