简体   繁体   English

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

[英]Issue trying to use JsonNode to access an external secured RESTful service

I am writing a Java class to access a third-party public REST API web service, that is secured using a specific APIKey parameter. I am writing a Java class to access a third-party public REST API web service, that is secured using a specific APIKey parameter.

I can access the required Json array, using the JsonNode API when I save the json output locally to a file.当我将 json Z78E6221F6393D14CEDZ86 本地保存到文件时,我可以使用 JsonNode API 访问所需的 Json 数组。

Eg例如

JsonNode root = mapper.readTree(new File("/home/op/Test/jsondata/loans.json"));

But, if I try to use the live secured web URL with JsonNode但是,如果我尝试使用带有 JsonNode 的实时安全 web URL

Eg例如

JsonNode root = mapper.readTree(url);

I am getting a:我得到一个:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60))

which suggests that I have a type mismatch.这表明我有类型不匹配。 But I am assuming that it is more likely to be a connection issue.但我假设它更有可能是连接问题。

I am handling the connection to the REST service:我正在处理与 REST 服务的连接:

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

I have also tried to use:我也尝试过使用:

URL url = new URL(surl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();       
InputStream isr = httpcon.getInputStream();
JsonNode root = mapper.readTree(isr);

with the same result.结果相同。

When I remove the APIKey I receive a Status 400 Error.当我删除 APIKey 时,我收到状态 400 错误。 So I think I must not be handling the APIKey parameter.所以我想我一定不能处理 APIKey 参数。

Is there way to handle the call to the secured REST service URL using JsonNode?有没有办法使用 JsonNode 处理对安全 REST 服务 URL 的调用? I would like to continue to use the JsonNode API, as I am extracting just two key:value pairs traversing multiple objects in a large array.我想继续使用 JsonNode API,因为我只提取两个键:遍历大型数组中多个对象的值对。

Just try to simply read response into string and log it to see what's actually going on and why you do not receive JSON from server.只需尝试简单地将响应读入字符串并记录它以查看实际发生的情况以及为什么您没有从服务器收到 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM