-1

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.

E.g.

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

E.g.

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:

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. So I think I must not be handling the APIKey parameter.

Is there way to handle the call to the secured REST service URL using JsonNode? 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.

Tom
  • 16,842
  • 17
  • 45
  • 54
Beckteck
  • 65
  • 1
  • 10
  • Can I ask the reason for the Down Vote? I asked what I thought was a reasonable question - I am relatively new to Java and never written a class to consume a rest API before in any language. As a result of @Ivan's answer I was able to complete my class successfully. Surely that is the point of SO. I'm not a regular user and dv'ing without any explanation does not help me become a better question asker or a better programmer. Or am I completely missing the point? – Beckteck Oct 05 '19 at 14:04

1 Answers1

1

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.

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
}
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • Thank you for that snippit. That's really interesting, the response is coming back in XML format: I am assuming I now have to work out how to convert the input into Json? – Beckteck Oct 02 '19 at 15:41
  • 1
    @Beckteck you can try to set header `Accept` to `application/json` to notify remote server that you need JSON (if remote server can provide JSON as response) – Ivan Oct 02 '19 at 15:54
  • that is what I was thinking. Thank you. – Beckteck Oct 02 '19 at 16:19