RestTemplate and acessing json

I have seen the answers of many other posts, but would like to understand if there is a better way to do the same.

Requirement: - I use restTemplate to talk to a web service that returns JSON output, which is dynamic. As a consumer, I do not want to access all the fields, but I am interested in a few of them. I use Spring framework with Jackson parser and found a way to access it

     String  response = restTemplate.getForObject(targetUrl, String.class);
     System.out.println(response);
     ObjectMapper mapper = new ObjectMapper();
     JsonNode rootNode = mapper.readValue(response, JsonNode.class);
     JsonNode uri = rootNode.get("uri");
     System.out.println(uri.asText());

Do you know the best way to do this? Mapping to java Object is what I don't want to do, as json output is not in my control

+4
source share
1 answer

RestTemplate HttpMessageConverters, Jackson2ObjectMapperBuilder, JsonNode restTemplate.getForObject.

,

ArrayNode resources = restTemplate.getForObject("/resources", ArrayNode.class);

ObjectNode resource = restTemplate.getForObject("/resources/123", ObjectNode.class);

, ArrayNode ObjectNode JsonNode.

0

Source: https://habr.com/ru/post/1548179/


All Articles