0

I have a requirement where I need to fetch the response of various company results. Each company will share a REST API with me (with all the details to invoke the API). I need to read a particular param from the response. The param will be known to me in advance and it might differ for every API.

Since the response for all the APIs will be different, I am looking for a generic method of calling the URL (always GET) and parsing the response in a JSON format and read the output param.

I tried doing the following:

String response = restTemplate.getForObject(URL,request,String.class);

But this fails to invoke the API. I also tried doing:

Object response = restTemplate.getForObject(URL,request,Object.class);

and

JsonNode response = restTemplate.getForObject(URL,request,JsonNode.class);

This also doesn't work. Is there any other way so that I can invoke the API and convert its response in a JSON object and read a particular property of the JSON.

I am not an expert but is this feasible in Java (I have done something similar with JS, but ofcourse it has much relaxed datatypes).

Thanks for any inputs.

1
  • what do u mean doesn't work ? can u post the error ? Commented Apr 30, 2024 at 18:35

1 Answer 1

0

This has always worked for me:

String jsonStr = restTemplate.getForObject (
  "/search?keywords={keywords}",
  String.class,
  "my search string"
);

Note the match between the {keywords} placeholder and the Java third parameter.

Your examples look suspect, for they have a request object in a getForObject() method. The latter sends a GET request and it's unusual to pass a request object in this kind of HTTP request. In fact, the method's signature is: getForObject(url, responseType, urlVariables) and other variants, where there isn't any request to be sent.

So, either you don't actually have a request object (only URL parameters), or you have POST methods, which require restTemplate.postForObject(), or, unlikely, you have a GET method that accept a payload in the request.

Spring doesn't have a getForObject() variant for the latter case, because, as said, that's unusual. If you really have such a method and you or others don't want to turn it into a POST method (but they should), you should be able to send such a weird request via RestTemplate.exchange() or .execute(), but I've never tried it.

Once you have the correct method and invocation, the variant asking for a JsonNode should work too.

Sign up to request clarification or add additional context in comments.

4 Comments

I thoughtthat this would work for me, but I reckon that String.class doesn't work if the response type is application/json. In my case all APIs are responding with Content-Type: application/json
String.class should cause JSON (or whatever output) to be translated to a String, the problem seems to be this request you're passing to a GET. Show us details for more help.
Okay, the issue with my code was that the API I was trying to hit was expecting certain headers. The request parameter in my getForObject was me trying to pass some headers around (headers creation code not in original question). I ultimately tried with a basic API of my own and your code works for it.
RestTemplate.exchange() or .execute() are lower-level calls, which should allow for setting details like headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.