简体   繁体   English

使单个 Play Framework 2.6 操作处理路由而忽略其 HTTP 方法的最佳方法

[英]Best way to make a single Play Framework 2.6 action handle a route disregarding its HTTP method

We have a Play 2.6 Java application that works great so far.到目前为止,我们有一个运行良好的 Play 2.6 Java 应用程序。 Now we need to add some route to it that will just be taking the original request, decorating it with some additional headers or whatever and sending it forward to be handled by some other app (probably using Play's WS client) The question is if it is possible to create some route like this one in the Play routes file:现在我们需要向它添加一些路由,它只会接受原始请求,用一些额外的标头或其他东西装饰它,然后将其转发给其他应用程序处理(可能使用 Play 的 WS 客户端)问题是它是否是可以在播放路线文件中创建一些这样的路线:

* /forward-to-smth/*whatever my.Action.forward

where * will match all the HTTP Methods, and we'll be just getting the Http.Request instance in the action body and handling it as required.其中 * 将匹配所有 HTTP 方法,我们将在操作正文中获取 Http.Request 实例并根据需要处理它。

What is the best way of handling this kind of scenarios in Play?在 Play 中处理此类场景的最佳方法是什么? We can probably create multiple routes' entries for each HTTP method and multiple actions in in the controller for each of the methods, delegating all the handling to a single handle-it-all method, but is there any more elegant solution?我们可能可以为每个 HTTP 方法创建多个路由条目,并在控制器中为每个方法创建多个动作,将所有处理委托给一个单一的 handle-it-all 方法,但有没有更优雅的解决方案?

Creating some custom HttpRequestHandler for this case will probably be an overkill?为这种情况创建一些自定义的HttpRequestHandler可能会有点矫枉过正?

Play conveniently generates a Router from the routes file, but you can also write a router yourself. Play 很方便地从路由文件生成一个路由器,但你也可以自己编写一个路由器。 See https://www.playframework.com/documentation/2.6.x/ScalaSirdRouter请参阅https://www.playframework.com/documentation/2.6.x/ScalaSirdRouter

class MyRouter @Inject()(controller: MyController) extends SimpleRouter {
  override def routes: Routes = {
    case _ => controller.forward
  }
}

Then, in your routes file, add然后,在您的路由文件中,添加

-> /forward-to-smth  my.MyRouter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM