0

Greetings Spring Experts,

I am new to spring and trying to invoke an api using RestTemplate from my @Service class. I will be getting the response in the from of JSON.

I followed the json parsing with resttemplate to create my POJOs from the JSON response and used the following code to invoke the Rest API.

ResponseEntity<BackendAPI> response = restTemplate.exchange(URL,
            HttpMethod.GET, requestEntity, BackendAPI.class);

Then using the getter setters I am extracting the values from BackendAPI class and creating the JSON Object FrontendAPI.class to send the response to the caller of my sprint boot api.

Now there is another way of doing the same without manually creating the POJOs rather using ObjectMapper to dynamically create the JSON Object from the backend api response and traversing through the nodes to retrieve the values to create the FrontendAPI.class object as below.

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");

My JSON response from backend is similar to below

{
"@odata.context": "some context value here",
"value": [{
    "@odata.id": "odata id value1",
    "@odata.etag": "W/\"CQEet/1EgOuA\"",
    "Id": "id1",
    "Subject": "subject1"
}, {
    "@odata.id": "odata id value2",
    "@odata.etag": "W/\"CyEet/1EgOEk1t/\"",
    "Id": "id2",
    "Subject": "subject2"
}]

}

And I have 10 such endpoints to deal with.

So my real concern is by creating the POJOs from JSON and using resttemplate.exchange method to automap the response to POJO am I creating a performance hole?

In other words using ObjectMapper to dynamically map the response is better / faster way over using pre-defined POJOs?

Please Note, my backend api response object is static and the fields will not change.

Thanks in advance for your help.

BKA
  • 119
  • 1
  • 6
  • your object mapper can map the item directly on the pojo `.mapper.readValue(theJson, YourPojo.class )` which is actually the most common use case – Michael Michailidis Sep 03 '19 at 08:22
  • Greetings Michael, thanks for that. My question was more about not creating the POJO at all and using the ObjectMapper to retreive the JSON response. I am happy to create the POJOs but is that best way or accessing the JSON from ObjectMapper without creating the POJOs is the bestway? Thanks. – BKA Sep 03 '19 at 08:27
  • Ah you mean using the JsonNode directly. Well this is not a good practice especially when your model is 100% defined and you dont have unknown fields. Having the values mapped on a pojo will add readability to your code from you and the people that will later need to read your code. So instead of guessing / traversing the nodes they will have a defined model. – Michael Michailidis Sep 03 '19 at 08:30
  • Greetings Michael, yeah I too thought the same but I was told using ObjectMapper JsonNode is more faster and performance effective rather than using the POJOs with resttemplate.exchange() method. Also under the hoods the resttemplate.exchange() is doing the same serialization mechanism. Hence I am little confused how to prove this, so posted here for experts answers. Thanks. – BKA Sep 03 '19 at 08:34
  • thats why i am not answering and i use comments. In my opinion mapping on a pojo is the way . but can't answer the performance part ! – Michael Michailidis Sep 03 '19 at 08:36

0 Answers0