Designing HTTP Bulk Request

2019-08-25 18:30发布

问题:

I am designing a REST Bulk Request that will take following array of following element:

{
    "method": <POST | GET | PATCH>,
    "path": <Relative URL of API to execute>
}

All these Bulk elements would execute the API on the same server. Is there a way I can call dispatcher servlet to execute this internally rather than calling methods backed by API ? I also want to read response for every request operation in Bulk request and accumulate response that will returned.

Currently I am calling methods directly with if elses doing request mapping style of work.

Please let me know if I am missing any details on expressing my problem definition. Request redirect/forward wont work in my case as I need to be in control to execute every operation in Bulk request and collect and accumulate response.

I am using Spring Boot MVC

Anand

回答1:

You can consider implementing "dummy" HttpServletRequest and HttpServletResponse. Autowire the DispatcherServlet in your bulk request controller.

In your controller method which handle the bulk request, while looping through the individual request, instantiate your own implementation of HttpServletRequest and HttpServletResponse and call the

dispatcherServlet.service(request, response);

You can read the response status via response.getStatus() and content via response.getOutputStream() or response.getWriter(). See java-printwriter-vs-servletoutputstream/.

By doing so, you don't have to manually call your controller method manually.

That's how I implemented the bulk requests.