Play framework Route case insensitive

你说的曾经没有我的故事 提交于 2020-01-04 04:58:06

问题


We are currently working on Play 2.5.x

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

GET /via/v1/organizations http.organizationApi()

In the URL we wanted to achive

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

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

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


回答1:


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)
  }
}

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"

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

There are two caveats though:

  • 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)
    

    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.



来源:https://stackoverflow.com/questions/38870551/play-framework-route-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!