好一些對象類型的路由參數自己的對象的粘合劑,我想,以取代從以下播放斯卡拉路線我的字符串PARAM到我自己的對象,說「爲MyObject」實現在播放階
From GET /api/:id controllers.MyController.get(id: String)
To GET /api/:id controllers.MyController.get(id: MyOwnObject)
任何如何做到這一點的想法將不勝感激。
好一些對象類型的路由參數自己的對象的粘合劑,我想,以取代從以下播放斯卡拉路線我的字符串PARAM到我自己的對象,說「爲MyObject」實現在播放階
From GET /api/:id controllers.MyController.get(id: String)
To GET /api/:id controllers.MyController.get(id: MyOwnObject)
任何如何做到這一點的想法將不勝感激。
使用PathBindable從路徑而不是從查詢綁定參數。
public class CommaSeparatedIds implements PathBindable<CommaSeparatedIds> {
private List<Long> id;
@Override
public IdBinder bind(String key, String txt) {
if ("id".equals(key)) {
String[] split = txt.split(",");
id = new ArrayList<>(split.length + 1);
for (String s : split) {
long parseLong = Long.parseLong(s);
id.add(Long.valueOf(parseLong));
}
return this;
}
return null;
}
...
}
樣品路徑:
/data/entity/1,2,3,4
樣品路線條目:綁定來自路徑ID用逗號(沒有錯誤處理)分離的樣品實施
GET /data/entity/:id controllers.EntityController.process(id: CommaSeparatedIds)
我不確定它是否適用於綁定URL路徑部分的數據,但如果您能夠接受數據作爲查詢參數,則可能需要閱讀QueryStringBindable
上的文檔。
好了,我已經寫了現在我自己的「MyOwnObject」活頁夾。另一種實現PathBindable來綁定對象的方法。
object Binders {
implicit def pathBinder(implicit intBinder: PathBindable[String]) = new PathBindable[MyOwnObject] {
override def bind(key: String, value: String): Either[String, MyOwnObject] = {
for {
id <- intBinder.bind(key, value).right
} yield UniqueId(id)
}
override def unbind(key: String, id: UniqueId): String = {
intBinder.unbind(key, id.value)
}
}
}