Java 尝试使用JsonNode访问外部安全RESTful服务时出现问题

Java 尝试使用JsonNode访问外部安全RESTful服务时出现问题,java,json,jackson,jsonnode,Java,Json,Jackson,Jsonnode,我正在编写一个Java类来访问第三方公共RESTAPI web服务,该服务使用特定的APIKey参数进行保护 在本地将Json输出保存到文件时,我可以使用JsonNode API访问所需的Json数组 例如 但是,如果我尝试将实时安全web URL与JsonNode一起使用 例如 我得到一个: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)) 我还尝试使用: URL ur

我正在编写一个Java类来访问第三方公共RESTAPI web服务,该服务使用特定的APIKey参数进行保护

在本地将Json输出保存到文件时,我可以使用JsonNode API访问所需的Json数组

例如

但是,如果我尝试将实时安全web URL与JsonNode一起使用

例如

我得到一个:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60))
我还尝试使用:

URL url = new URL(surl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();       
InputStream isr = httpcon.getInputStream();
JsonNode root = mapper.readTree(isr);
同样的结果

当我删除APIKey时,我收到一个状态400错误。所以我想我一定不是在处理APIKey参数


有没有办法使用JsonNode处理对安全REST服务URL的调用?我想继续使用JsonNode API,因为我只提取了两个键:值对,它们在一个大数组中遍历多个对象。

只需尝试将响应读入字符串并记录它,就可以了解实际情况以及为什么不从服务器接收JSON

URL url = new URL(surl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();       
InputStream isr = httpcon.getInputStream();
try (BufferedReader bw = new BufferedReader(new InputStreamReader(isr, "utf-8"))) {
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bw.readLine()) != null) { // read whole response
        sb.append(line);
    }
    System.out.println(sb); //Output whole response into console or use logger of your choice instead of System.out.println
}

谢谢你的嘲笑。这真的很有趣,响应以XML格式返回:我假设我现在必须解决如何将输入转换为Json?@becketck您可以尝试将header
Accept
设置为
application/Json
以通知远程服务器您需要Json(如果远程服务器可以提供Json作为响应)我就是这么想的。谢谢。我能问一下否决票的原因吗?我问了一个我认为合理的问题——我对Java比较陌生,以前从未用任何语言编写过使用RESTAPI的类。由于@Ivan的回答,我成功地完成了我的课程。当然,这就是问题的关键所在。我不是一个普通用户,没有任何解释的dv'ing不能帮助我成为一个更好的提问者或更好的程序员。还是我完全没有抓住重点?
private static String surl = "https://api.rest.service.com/xxxx/v1/users/xxxxx/loans?apikey=xxxx"
public static void main(String[] args) {

    try {

        URL url = new URL(surl);
        JsonNode root = mapper.readTree(url);
        ....
     }
URL url = new URL(surl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();       
InputStream isr = httpcon.getInputStream();
JsonNode root = mapper.readTree(isr);
URL url = new URL(surl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();       
InputStream isr = httpcon.getInputStream();
try (BufferedReader bw = new BufferedReader(new InputStreamReader(isr, "utf-8"))) {
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bw.readLine()) != null) { // read whole response
        sb.append(line);
    }
    System.out.println(sb); //Output whole response into console or use logger of your choice instead of System.out.println
}