2

I want to be able to have this:

POST /items controllers.Application.update()
POST /items/:itemType controllers.Application.update(itemType: String)
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)

but that doesn't compile due to the error of method update is defined twice. Then I changed it and it didn't compiler either:

POST /items controllers.Application.update(itemType: Option[String] = None, id: Option[Int] = None)
POST /items/:itemType controllers.Application.update(itemType: String, id: Option[Int] = None)
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)

the errors are:

  1. the previouse one
  2. and type mismatch; found: Option[String]; required: String

What do I do about that? I wouldn't like to do something like this:

POST /items controllers.Application.updateAll()
POST /items/:itemType controllers.Application.updateByType(itemType: String)
POST /items/:itemType/:id controllers.Application.updateByTypeAndId(itemType: String, id: Int)

and this doesn't look good either since I'd like to use Option instead of the empty string:

POST /items controllers.Application.update(itemType: String = "", id: Int = "")
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int = "")
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)
4

2 回答 2

1

如果您能够更改 URL 格式,则可以使用 Option。

路线:POST /items controllers.Application.update(itemType: Option[String], id: Option[Int])

网址:http://localhost:9000/items?itemType=someItem&id=123

使用这种格式,您可以在进行 Web 服务调用时省略 itemType、id 或两者。

于 2013-10-11T13:29:47.180 回答
1

不幸的是,似乎在 v2 中删除了对 Option 的支持 - 例如,请参见此处- 所以您可能会被困在自己编写PathBindable处理 Options 的代码(如上面的链接中所述),或者您注意到的其他令人讨厌的选择之一。

于 2013-10-11T03:29:53.500 回答