Play!Framework中的批量HTTP请求 [英] Batch HTTP requests in Play!Framework
问题描述
我已实现当前的一组路由(例如):
I have the current set of of routes implemented (for example):
GET /api/:version/:entity my.controllers.~~~~~
GET /api/:version/:entity/:id my.controllers.~~~~~
POST /api/:version/:entity my.controllers.~~~~~
POST /api/:version/:entity/:id my.controllers.~~~~~
DELETE /api/:version/:entity my.controllers.~~~~~
POST /api/:version/search/:entity my.controllers.~~~~~
他们工作得很漂亮。现在让我们说我想为同一个API实现批处理终结点。它看起来应该是这样的:
And they work beautifully. Now let's say I want to implement a "batch endpoint" for the same API. It should look something like this:
POST /api/:version/batch my.controllers.~~~~~
并且正文应如下所示:
[
{
"method": "POST",
"call": "/api/1/customer",
"body": {
"name": "antonio",
"email": "tonysmallhands@gmail.com"
}
},
{
"method": "POST",
"call": "/api/1/customer/2",
"body": {
"name": "mario"
}
},
{
"method": "GET",
"call": "/api/1/company"
},
{
"method": "DELETE",
"call": "/api/1/company/22"
}
]
为此,我想知道如何调用播放框架路由器来传递这些请求?我计划使用与单元测试建议类似的东西:
To do that I would like to know how I can call the play framework router to pass those requests? I was planning to use something similar as what is advised for unit tests:
@Test
public void badRoute() {
Result result = play.test.Helpers.routeAndCall(fakeRequest(GET, "/xx/Kiki"));
assertThat(result).isNull();
}
进入的源代码> routeAndCall()
,你会发现这样的事情:
by going into the source code of routeAndCall()
, you find something like this:
/**
* Use the Router to determine the Action to call for this request and executes it.
* @deprecated
* @see #route instead
*/
@SuppressWarnings(value = "unchecked")
public static Result routeAndCall(FakeRequest fakeRequest) {
try {
return routeAndCall((Class<? extends play.core.Router.Routes>)FakeRequest.class.getClassLoader().loadClass("Routes"), fakeRequest);
} catch(RuntimeException e) {
throw e;
} catch(Throwable t) {
throw new RuntimeException(t);
}
}
/**
* Use the Router to determine the Action to call for this request and executes it.
* @deprecated
* @see #route instead
*/
public static Result routeAndCall(Class<? extends play.core.Router.Routes> router, FakeRequest fakeRequest) {
try {
play.core.Router.Routes routes = (play.core.Router.Routes)router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null);
if(routes.routes().isDefinedAt(fakeRequest.getWrappedRequest())) {
return invokeHandler(routes.routes().apply(fakeRequest.getWrappedRequest()), fakeRequest);
} else {
return null;
}
} catch(RuntimeException e) {
throw e;
} catch(Throwable t) {
throw new RuntimeException(t);
}
}
所以我的问题是:是否有一个hacky 使用Play(我不是反对混合使用Scala和Java来实现它)的方法比复制上面的代码要好吗?我还想提供并行或按顺序执行批量调用的选项...我想使用类加载器只实例化一个 Routes
会有问题吗?
So my question is: Is there a less "hacky" way to do this with Play (I am not against mixing Scala and Java to get to it) than to copy the above code? I would have also like to give the option of executing the batched calls in parallel or in sequence ... I guess instantiating only one Routes
using the class loader would be problematic then?
推荐答案
您可以使用以下方法调用来路由您的虚假请求:
播放。 current.global.onRouteRequest
。请参阅此帖子以获取完整示例: http://yefremov.net/blog/play-batch -api /
You can use the following method call to route your fake requests:
Play.current.global.onRouteRequest
. Please see this post for a full example: http://yefremov.net/blog/play-batch-api/
这篇关于Play!Framework中的批量HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!