We have set(6-8) of API and it will be invoked in a sequence . there are cases, want to use output of first/second API in the third/fourth API .

We have request object which is passed as input to all the APIs . we have defined all the response object in the same request and after each API execution corresponding response object will be set. what is the right way of passing the API outputs to the other API?

  public interface CeaseHandler<Request, Response> {

        public Response cease(final Request request);


    First API

    public class CircuitCeaseHandler implements CeaseHandler<DeletionRequest, GenericApiResponse> {
      public GenericApiResponse cease(final DeletionRequest request) {

    Fifth API

    public class PhysicalPortCeaseApiHandler implements CeaseHandler<DeletionRequest, GenericApiResponse>{
     public GenericApiResponse cease(final DeletionRequest request) {

Here DeletionRequest has set attr which is output of first api and will be used in fifth api. ideally request shouldn't hold any api response.


1 Answer 1


You could mimic the middleware pattern here like, having a context object that lives throughout the sequence of API calls and store all the intermediate results there along with request and response objects.

Or,if you are ok with bloating the response request object you could simply piggy-back the Intermediate results within the response request object too.

This technique is commonly used in Golang and NodeJS web frameworks that use the middleware-style of creating web apps(not to chain APIs but to chain functionality),But you could copy the technique and apply it for chaining API calls too.

UPDATE : You could also use a map to hold your intermediates within the response object. So you wouldn't have to worry much about changing your code much to accommodate your newer apis.

I am not a java expert but you should have Thread safe Map implementation on JAVA (ConcurrentHashMap?) that you can utilize to go the thread safe way.

Here's a link that you might use as reference


  • Thanks Naresh. Either way ,I have to keep context or api response instance variable in Request object. Is that a correct way of designing an API. What I am thinking is request shouldn't hold anything about response attribute .if one more api is getting added later,changes must be minimal.
    – Selva
    Dec 5, 2016 at 5:46
  • I'm sorry my bad. I was supposed to mean the response object and not request (I'll update my answer). Yep.Handling Changes would be a pain. You could use a Map that can hold various intermediates for you. This way you wouldn't have to change your code much to support adding new functionality.
    – onkkno
    Dec 5, 2016 at 8:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.