Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用web服务在Java中接收json_Java_Json_Service_Web - Fatal编程技术网

使用web服务在Java中接收json

使用web服务在Java中接收json,java,json,service,web,Java,Json,Service,Web,我使用Java调用服务器以接收json信息,并希望通过Rest Web服务检索该信息。现在我有这个代码: public class consult { public static void consult1() the url that retrieves json); InputStream response = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReade

我使用Java调用服务器以接收json信息,并希望通过Rest Web服务检索该信息。现在我有这个代码:

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

实际上,您真的应该将InputStream从响应直接输送到输出流。在TUS中,IOUtils/DataFetcher中有自动执行此管道的工具:

我建议您查看本教程,了解如何实现RESTful JSON服务:

正确设置背后的思想是不需要从方法返回序列化字符串。您只需返回一个域对象,并指示框架通过注释对其进行序列化


当然,您会发现关于如何发布新对象或更新的示例(JSON)。

但是我将通过这个web服务接收更多的JSON信息,我不想为每次咨询实现一个Bean。我只想返回我已经拥有的json。