设计 HTTP 批量请求 [英] Designing HTTP Bulk Request
问题描述
我正在设计一个 REST 批量请求,它将采用以下元素的以下数组:
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>
}
所有这些 Bulk 元素都将在同一服务器上执行 API.有没有办法可以调用调度程序 servlet 在内部执行它而不是调用 API 支持的方法?我还想读取批量请求中每个请求操作的响应并累积将返回的响应.
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.
目前我正在使用 if elses 执行请求映射工作方式直接调用方法.
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.
我正在使用 Spring Boot MVC
I am using Spring Boot MVC
阿南德
推荐答案
您可以考虑实现虚拟"HttpServletRequest 和 HttpServletResponse.在批量请求控制器中自动装配 DispatcherServlet.
You can consider implementing "dummy" HttpServletRequest and HttpServletResponse. Autowire the DispatcherServlet in your bulk request controller.
在处理批量请求的控制器方法中,在循环处理单个请求时,实例化您自己的 HttpServletRequest 和 HttpServletResponse 实现并调用
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);
dispatcherServlet.service(request, response);
您可以通过 response.getStatus() 读取响应状态,通过 response.getOutputStream() 或 response.getWriter() 读取内容.请参阅java-printwriter-vs-servletoutputstream/.
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.
这篇关于设计 HTTP 批量请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!