LoginSignup
4
4

More than 5 years have passed since last update.

Spray-routingでpathPrefixを使ってみた

Last updated at Posted at 2015-04-09

Spray-routingのpathPrefixを使う

Scalaでroutesを設定できるSpray-routingのpathPrefixを使ってみました。
設定したいroutesが下記の通りだとすると
GET /users
GET /users/:user_id
GET /users/:user_id/foo

pathのみを使って書くと

path("users") {
  get {
    //処理
  }
} ~
path("users" / ".+".r) { user_id =>
  get {
    //処理
  }
} ~
path("user" / ".+".r / "foo") { user_id =>
  get {
    //処理
  }
}

と書け、pathPrefixを使うと

pathPrefix("users") {
  pathEnd {
    get {
      //処理
    }
  } ~
  pathPrefix(".+".r) { user_id =>
    pathEnd {
      get {
        //処理
      }
    } ~
    path("foo") {
      get {
        //処理
      }
    }
  }
}

と書けました。個人的には下で書くほうが良いのかな?と思いましたがどうなんでしょう詳しい人教えてください。

4
4
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
4
4