play-framework 2 how to escape colon in routes

2019-07-20 13:52发布

问题:

in my play2 routes file, I am trying to use a colon as a literal:

GET     /:search                       controllers.SearchController.index()

but play complains, that a parameter is missing. How do I escape the colon (I tried backslashing it)?

thanks

回答1:

You must introduce a dummy regex parameter, as such:

GET     /$colon<\:>search           controllers.SearchController.index(colon)

You must then also redefine your controller method:

public static Result index(String colon) {
 ....

The parser is built in such a way that path expressions cannot be escaped, save for this method.