• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

POJO vs JsonNode with ObjectMapper which is faster using resttemplate to call an API?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1168
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't misunderstand me, but I think that Spring RESTTemplate exactly does that, i.e parses a low-level Json string and populates fields of a POJO, and I'm also pretty sure it does the work far better than any code that you or me could write. Generally speaking, if a framework offers you some facility, it's better to use it instead trying an handmade solution -unless there are very, very good reasons to choose another approach.
 
You get good luck from rubbing the belly of a tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!