Play Framework 2.2.1 - Case insensitive routing

我与影子孤独终老i 提交于 2019-12-23 09:37:53

问题


I am novice to the Play and currently working Play 2.2.1

I am trying to acheive case insensitive routing for my endpoints which are defined in "routes"

e.g. I have define an route say /accessLicense in routes file, it would look like below

GET /accessLicense controller.MyController.accessLicense()

Now, if I fire /accessLicense it woks great; as expected, but if try to fir /AccessLicense, /AcCeSSLicenSe or any other combination of upper/lower case letter which spell exact same word, it doesn't work.

Thanks in advance for guidance and support!!!


回答1:


Unfortunately, AFAIK, there is no way to magically turn on a switch that will do what you want. Thankfully, there's a workaround, inferior IMHO but its the best that can be done.

GET /[aA][cC][cC][eE][sS][sS].....

EDIT: I did the following, which matches my specific requirement of lower-casing only the first part of URL. So GET /AbCdE/XyZ will become GET /abcde/XyZ and if this has an action in the routes than it will be handled appropriately.

override def onRouteRequest( request: RequestHeader ) = {
   val path = request.path
   val split = path.split( "/" ).toList

   val lowerCasePath = split match{
     case ""::Nil  => ""::Nil
     case ""::x::y => ""::x.toLowerCase::y
   }

   logger.error( lowerCasePath.toString )

   super.onRouteRequest( request.copy( path = lowerCasePath.mkString( "/" ) ) )
}

EDIT See here: https://jazzy.id.au/2013/05/08/advanced_routing_in_play_framework.html



来源:https://stackoverflow.com/questions/22015902/play-framework-2-2-1-case-insensitive-routing

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