我使用Java调用服务器以接收json信息,我想通过Rest Web Servie检索该信息。现在我有这个代码:
public class consult {
public static void consult1() the url that retrieves json);
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
reader.close();
}
这在控制台上完美地显示了Json但是如何将信息设置为某个变量,所以当我在浏览器中调用此WS(/ resources / consult /)时,根据此WS显示信息?
@Path("/consult")
public class ConsultJSON {
@GET
@Produces ("application/json")
public String getInfo() throws MalformedURLException, IOException {
return consult.consult1();
}
答案 0 :(得分:1)
我建议您查看本教程,了解如何实现RESTful JSON服务:
http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/
正确设置背后的想法是,您不需要从您的方法返回序列化字符串。您只需返回一个域对象,并指示框架通过注释对其进行序列化。
当然,您可以找到有关如何为其发布新对象或更新的示例(以JSON格式)。
答案 1 :(得分:0)
实际上,您确实应该将InputStream从响应直接传递到OutputStream。在IOUtils / DataFetcher的TUS中有自动执行此管道的工具:http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/