In my code, I am using RestTemplate to call an external API and receive a complex JSON response. However, for my specific use case, I only need to return the JSON as-is to the front-end. To achieve this, I have chosen to map the response to a Map<String, Object>.
Currently, I have observed that the mapping works well, and during debugging, I can see that it returns a LinkedHashMap, which suits my requirements as I need the values to be ordered. However, I would like to confirm if this behavior is consistent and reliable.
My question is: How can I determine if the mapping operation will always return a LinkedHashMap or if it could potentially return a different implementation, such as a HashMap?
I have searched for relevant documentation but haven't been able to find any specific information regarding the implementation returned by RestTemplate when mapping JSON responses to a Map.
Any insights or pointers to relevant documentation would be greatly appreciated. Thank you!
Here's my code:
ParameterizedTypeReference<Map<String, Object>> responseType = new ParameterizedTypeReference<Map<String, Object>>() {};
return restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<>(request, getHeaders()), responseType).getBody();
Edit: i tried to convert the response to a String but i got the error:
Error while extracting response for type [class java.lang.String] and content type [application/json]
Here's my new code:
return restTemplate.postForObject(uri, new HttpEntity<>(request, getHeaders()), String.class);