Play 2.0.4 Catch-all route always hits

2019-06-03 17:34发布

问题:

At the end of my routes file I put a catch-all route to catch requests which wasn't catch previously and pass it to my own router (for further processing):

GET    /*nameUrl      controllers.Application.router(nameUrl: String)

of course there are many other routes BEFORE that line. The big surprise for me is that the catch-all is hitten every time, even if previous route is hitten as well, so if I'm opening address domain.tld/test it displays me both logs in the console Test action hit! AND Custom router hit! . There is a simplified sample:

public static Result test() {
    Logger.debug("Test action hit!");
    return ok();
}

public static Result router(String nameUrl) {
    Logger.debug("Custom router hit!");
    return ok();
}

Routes (in this order)

GET    /test          controllers.Application.test
GET    /*nameUrl      controllers.Application.router(nameUrl: String)

What do I want to get:

I want to get url's for articles with my router ie domain.tld/category_1/article_title without any prefix before it, of course if I change catch all to something stable it won't get double hits anymore:

GET    /news/*nameUrl      controllers.Application.router(nameUrl: String)
domain.tld/news/category_1/article_title

however I really want to avoid /news/ segment. Is that possible?

回答1:

I repeated it and had the same problem with Chromium (core of Google Chrome), but not with Firefox.

With Global.java I analyzed the request.

public class Global extends GlobalSettings {
    @Override
    public Action onRequest(Http.Request request, Method method) {
        Logger.info("request-path: " + request.path());
        return super.onRequest(request, method);
    }
}
//output:
[info] application - request-path: /favicon.ico

For every GET /test request Chromium tries to load the favicon.

So, include the following in conf/routes:

GET  /favicon.ico   controllers.Assets.at(path="/public", file="favicon.ico")