播放框架Route不区分大小写 [英] Play framework Route case insensitive

查看:100
本文介绍了播放框架Route不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在开发Play 2.5.x

We are currently working on Play 2.5.x

我们想实现不区分大小写的路由.例如

We wanted to achive case insensitive routing to be done. Say for example

获取/via/v1/organizations http.organizationApi()

我们要在URL中达到

http://localhost:9000/abc/v1/organizations

http://localhost:9000/abc/v1/organizations

http://localhost:9000/ABC/V1/OrganIZations

http://localhost:9000/ABC/V1/OrganIZations

是使用正则表达式实现此bu的一种方法吗?可以指出一个例子或给我一个例子吗?

Is the a way to achive this bu using regular expression? Can some one point to or provide me an example?

推荐答案

您可以定义请求处理程序以使URL不区分大小写.在这种情况下,以下处理程序将url转换为小写,因此在您的路由中,应将URL定义为小写:

You can define a request handler to make the URL case insensitive. In this case, the following handler will just convert the url to lowercase, so in your routes the url should be defined in lowercase:

import javax.inject.Inject

import play.api.http._
import play.api.mvc.RequestHeader
import play.api.routing.Router

class MyReqHandler @Inject() (router: Router, errorHandler: HttpErrorHandler,
                   configuration: HttpConfiguration, filters: HttpFilters
          ) extends DefaultHttpRequestHandler(router, errorHandler, configuration, filters) {

  override def routeRequest(request: RequestHeader) = {
    val newpath = request.path.toLowerCase
    val copyReq = request.copy(path = newpath)
    router.handlerFor(copyReq)
  }
}

application.conf中使用以下代码进行引用:

And in application.conf reference it using:

# This supposes MyReqHandler.scala is in your project app folder
# If it is in another place reference it using the correct package name
# ex: app/handlers/MyReqHandler.scala --> "handlers.MyReqHandler"
play.http.requestHandler = "MyReqHandler"

现在,如果您有一个定义为"/persons/create"的路线,那么任何大小写组合都将起作用(例如:"/PeRsOns/cREAtE")

Now, if you have a route define to "/persons/create", whatever case combination will work (ex: "/PeRsOns/cREAtE")

尽管有两个警告:

  • 您只能将其与Scala操作一起使用.如果您的路由文件引用了Java控制器方法,则会出现一个奇怪的异常:

  • You can only use this with Scala actions. If your routes file references a Java controller method, you will get an odd exception:

[error] p.c.s.n.PlayRequestHandler - Exception caught in Netty
scala.MatchError: Right((play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3@22d56da6,play.api.DefaultApplication@67d7f798)) (of class scala.util.Right) 

如果是您的情况,您可以在此处

If this is your case you can find more info here

如果您的网址包含参数,则这些参数也会被转换.例如,如果您有这样的路线

If your url have parameters, those will also be transformed. For example, if you have a route like this

GET /persons/:name/greet       ctrl.Persons.greet(name: String)

对"/persons/JohnDoe/greet"的调用将转换为"/persons/johndoe/greet",并且您的greet方法将接收"johndoe"而不是"JohnDoe"作为参数.请注意,这不适用于查询字符串参数. 根据您的用例,这可能会出现问题.

a call to "/persons/JohnDoe/greet" will be transformed to "/persons/johndoe/greet", and your greet method will receive "johndoe" instead of "JohnDoe" as parameter. Note that this does not apply to query string parameters. Depending in your use case, this can be problematic.

这篇关于播放框架Route不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆