简体   繁体   中英

How to merge two ResponseEntity objects in Spring

I am using Spring 4.2. I've to make two separate http calls that return ResponseEntity<String> . However, I have to merge them before returning to the caller methods.

Their data structure is exactly similar. Just the content is different. How can I merge this two entities before returning back to the caller ?

Use rest template to get your entities inside the entity you want to merge.

final String uri = "http://localhost:8080/springrestexample/employees.xml";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);

    final String uri2 = "http://localhost:8080/springrestexample/employees2.xml";
    RestTemplate restTemplate2 = new RestTemplate();
    String result2 = restTemplate.getForObject(uri, String.class);

return result1+result2; //or just your mergeMethod(result1,result2)

You can also create a new endpoint with service that is merging the entities with Collections framework or Java 8 stream map.

You can also create a better repository data access object that is joining the tables on DB level - sql, hql, jpql.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM