How to implement restTemplate.getForObject with String class

I need help with this code, i’m implementing getForObject(url, String.class) with String class and Storing the value in a String, After that Using Obect Mapper readValue i’m converting it to TiingoCandle[].class, and its not working, but if i use TiingoCandle[].class directly without String class then its works fine, can anyone please help me with by pointing what am I missing while implementing with String.Class or I should not implement using String.class at all. Thank you in advance.
@anand-crio

could you be more Specific about the Error!!.. is it returning null or you are getting some error?

@jayanta as you said you are storing the JSON result from Tiingo as a string, its absolutely fine, no problem.

Now you said you are using that JSON result in object mapper and storing each substring of JSON in your created TiingoCandle POJO class, this is also a correct procedure.

I think the problem here is,

  1. Maybe you are not getting the result from Tiingo, I suggest you check your API request in a separate browser window.

  2. Or take a re-look at your POJO, maybe some parameters were mismatching.

Also as @yuva suggested, If you’re getting null resttemplate, then you need to do something with the constructor.

if I use TiingoCandle[].class, then it works fine, but if I use String.class it throws null pointer exception

My url is correct as I have tested it with postman.
And it also works fine if I use TiingoCandle[].class instead of String.class, but i have to use String.class for getting test success otherwise i am fine with TiingoCandle.

May I ask where you’re making all these changes ?

Hey @jayanta, kindly attach your code snippet for the reference.

I had this problem .Hope it helps!!

In string date is stored(along with time) but,the getDate() takes LocalDate as parameter.
if this problem exists . ping me if you need help

Hi @jayanta,

The way I understand it, it might be possible that when you parse the string to object mapper, the date may be the cause of the error. It might be due to the fact that the date may be have a time with it, or the fact that you are trying to map a string to LocalDate type variable. Both these seem less probable, but worth a shot when trying to debug.

You can also check your POJO object once. It might be possible that despite working with Tiingo[].class, your POJO may not be correctly implemented which is impeding the object mapper and giving you the error.

Let me know if the issue persists!

i see, that is why the problem is persisting, so should make my code compatible with date only ?

hm, my pojo object is fine as if i use pojo in getForObject it works fine.

i am making all these changes in tiingoService class

String s = restTemplate.getForObject(url, String.class);
TiingoCandle[] t = mapper.readValue(s, TiingoCandle[].class);

in this code im getting error, its returning null pointer, but if i use like this:
TiingoCandle[] t = restTemplate.getForObject(url, TiingoCandle[].class);
then its works fine.

@jayanta, try to instantiate objectMapper from getObjectMapper() method which is registered with JavaTimeModule().
This will work!

ohk, im trying with method

@yuva, @shoryajain, thank you for helping, it was happening because of that date with time, I checked in postman, it returns an Instant of time.
@imbipulkumar, thank you for your idea about using getObjectMapper() for such a simple solution, otherwise i was going to implement a pojo to handle that and again convert it back to tiingoCandle and etc.

1 Like

@jayanta and fellow students, this is how I solved this problem:
use the following code to make an API call and get the result as String:

String reply = this.restTemplate.getForObject(request, String.class);

Now parse this string using Jackson Library.
this code will convert the reply string into JsonNode object:

  // create object mapper
  ObjectMapper mapper = new ObjectMapper();
  // convert to json object
  JsonNode jsonNode = mapper.readTree(reply);

Now proceed to work with this JSON object and parse it to get the list of Candle.

Source: https://codippa.com/how-to-convert-json-string-to-json-object-in-java/
https://stackoverflow.com/questions/48785312/get-all-the-keys-in-a-json-string-jsonnode-in-java

PS: All the problems we are facing today are not new, they have existed for a long time now and so does their solutions. We just need to google stuff properly.

hope it helps :slightly_smiling_face:
2020-03-12T18:30:00Z

1 Like