2
votes

I have a general REST API (developed using Spring MVC) that takes a list of API requests as its request body. Each API request in the list has its own URL and request body.

In the implementation of this general REST API, I need to call the corresponding Spring controller method (in the same app) for each of these individual API requests (with their appropriate URL and request body). (I will then merge all those individual API responses and return it in one big response from the general REST API).

I've been searching around, but I'm unclear how to programmatically call Spring to execute each individual API request. I would ideally like to get back the ResponseEntity from each call instead of the actual JSON response.

(More information: On the same app server as the general API, I need to translate the URL and JSON request body for each individual API into the arguments to the controller method. I also need to take the URL and have Spring determine which controller method to invoke itself.)

Any help would be greatly appreciated.

Thanks,

Matt

1
I believe what you are trying to achieve is solved by implementing a micorservices architecture. - yogidilip
I agree with a microservice appoach, which is how I've done this in the past. We built an aggregate service that was its own deployable microservice. It had one job: upon receiving a request, it would invoke 4-5 downstream microservices in parallel and wait for them all to complete (or it would return whatever information it did get if one or more of them timed out). You can, of course, use RestTemplate to call back into the same app. Just be careful of resource limitations and making sure you have enough threads to handle the tomcat traffic. - Mike
RestTemplate from what I understand needs an absolute path and cannot take a relative one. So, what do you put for the full path? localhost:<port>? Seems less than ideal. - Matt

1 Answers

0
votes

Answer depend on whether the individual URLs that you are planning to invoke is with in the same server (Accessible without using network call) or not

  1. If it is with in the same app server, spawn multiple threads and invoke the individual methods and join the response together and send it back

  2. If it is not within the same app server, there are many Async Restclients are there besides spring's own webclient/restTemplate etc