Sparkのルーティング設定はワイルドカードが指定できるため、同じURLでも複数ルートにマッチングする可能性がある。
複数ルートにマッチングする場合は、先に定義したものが優先される仕組みのようだ。
また静的ファイルとも被る可能性があるが、そちらの優先度はJavaプログラムより低い。
のだが、index.htmlだけはなぜか順位が1番高い。この仕様は何なんだろう。
例えば、下記のプログラムがあると、
public class Main {
public static void main(String[] args) {
staticFiles.externalLocation(".");
get("/", (request, response) -> "hello index");
get("/sample", (request, response) -> "hello sample");
get("/sample.html", (request, response) -> "hello sample.html");
get("/:value", (request, response) -> "this is " + request.params("value"));
get("/sample2", (request, response) -> "hello sample2"); // これは呼ばれることはない
}
}
ルーティングは下記となる。
/ → 静的ファイルにindex.htmlが存在する場合はそちらが優先される。存在しない場合は "hello index" が表示される。
/index.html → 静的ファイルにindex.htmlが存在する場合はそちらが優先される。存在しない場合は "this is index.html" が表示される。
/sample → "hello sample" が表示される。
/sample2 → "this is sample2" が表示される。
/sample.html → "hello sample.html" が表示される。静的ファイルのsample.htmlは呼ばれない。
結構複雑です。