LoginSignup
3
0

More than 5 years have passed since last update.

Sparkでのルート評価の順番

Last updated at Posted at 2018-02-22

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は呼ばれない。

結構複雑です。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0