LoginSignup
2
0

More than 5 years have passed since last update.

Sparkのルーティングで仕様に迷うところ

Last updated at Posted at 2018-03-01

またsparkのルーティングの話です。
小出しになっているからそのうちまとめないと。

ワールドカードを指定した場合のルーティング

"/sample/*" というパスを設定した場合、下記のURLにマッチングします。

/sample/
/sample/hello
/sample/hello/
/sample/hello/*

下記にはマッチングしません。

/sample

before、after、afterAfterを使うときのリクエストパラメーター

パスパラメーターを利用する場合、filterでもマッチング設定を行う必要があります。

ダメなソース

public static void main(String[] args) {

  before((request, response) -> {
    system.out.println(request.params("name")); // nullが表示される
  });

  get("/:name", (request, respoonse) -> request.params("name"));
}

イケてるソース

public static void main(String[] args) {

  before("/:name", (request, response) -> {
    system.out.println(request.params("name")); // urlに指定された内容が表示される
  });

  get("/:name", (request, respoonse) -> request.params("name"));
}

ちょっと冗長な感じもしますね。

2
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
2
0