在Play scala中实现您自己的对象类型的Route参数的对象绑定器 [英] Implement your own object binder for Route parameter of some object type in Play scala
本文介绍了在Play scala中实现您自己的对象类型的Route参数的对象绑定器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗯,我想把我的String param从下面的Play scala中替换成我自己的对象,说MyObject
Well, I want to replace my String param from the following Play scala Route into my own object, say "MyObject"
From GET /api/:id controllers.MyController.get(id: String)
To GET /api/:id controllers.MyController.get(id: MyOwnObject)
任何想法如何做到这一点将不胜感激。
Any idea on how to do this would be appreciated.
推荐答案
使用 PathBindable 从路径而不是查询绑定参数。从逗号分隔的路径绑定ids的示例实现(无错误处理):
Use PathBindable to bind parameters from path rather than from query. Sample implementation for binding ids from path separated by comma (no error handling):
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;
}
...
}
$ b b
示例路径:
Sample path:
/data/entity/1,2,3,4
路线输入示例:
GET /data/entity/:id controllers.EntityController.process(id: CommaSeparatedIds)
这篇关于在Play scala中实现您自己的对象类型的Route参数的对象绑定器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文