斯卡拉电梯 + REST - 使用serveJx 返回数组
我有一个类扩展 RestHelper 以提供 xml 和 json 中的项目。目前,我正在实现“serve”方法来返回 xml 中的实体:
object Rest extends RestHelper {
serve {
case "supplier" :: "findAll" :: _ XmlGet _
=> supplierListToXml(Supplier.findAll)
}
def supplierListToXml(suppliers : List[Supplier]) = {
<suppliers>{suppliers.mapConserve(f=> f.toXml)}</suppliers>
}
}
我想根据 Accept-header 使用serveJx方法(在 http://www.assembla.com/wiki/show/liftweb/REST_Web_Services)。
类Supplier 实现了Convertable,并且我在Rest 类中实现了一个转换方法:
object RestApi extends RestHelper {
implicit def cvt: JxCvtPF[Convertable] = {
case (JsonSelect, c, _) => c.toJson
case (XmlSelect, c, _) => c.toXml
}
serveJx {
case Get("2.0" :: "supplier" :: "head" :: _,_)
=> Full(Supplier.findAll.head)
}
}
...当我为单个供应商提供服务时,这有效。但是,当我想返回供应商列表(List[Supplier])时,无法应用转换方法。
case Get("2.0" :: "supplier" :: "findAll" :: _,_)
=> Full(Supplier.findAll.toList)
...我得到编译错误:
“找不到隐含值 范围 cvt:com.mycompany.api.RestApi.JxCvtPF[ScalaObject]"
有人能给我一个示例,说明如何修改 cvt 方法以便能够将可转换列表转换为 LiftResponse 吗?
干杯! /J
I have a class that extends RestHelper to serve items in both xml and json. Currently, I am implementing the "serve"-method to return the entities in xml:
object Rest extends RestHelper {
serve {
case "supplier" :: "findAll" :: _ XmlGet _
=> supplierListToXml(Supplier.findAll)
}
def supplierListToXml(suppliers : List[Supplier]) = {
<suppliers>{suppliers.mapConserve(f=> f.toXml)}</suppliers>
}
}
I want to serve the same items as json aswell, depending on the Accept-header, using the serveJx-method (explained in http://www.assembla.com/wiki/show/liftweb/REST_Web_Services).
The class Supplier implements Convertable, and I implemented a conversion-method within the Rest-class:
object RestApi extends RestHelper {
implicit def cvt: JxCvtPF[Convertable] = {
case (JsonSelect, c, _) => c.toJson
case (XmlSelect, c, _) => c.toXml
}
serveJx {
case Get("2.0" :: "supplier" :: "head" :: _,_)
=> Full(Supplier.findAll.head)
}
}
... and this works find, when I serve a single supplier. But when I want to return a list of suppliers (List[Supplier]), the conversion-method cannot be applied.
case Get("2.0" :: "supplier" :: "findAll" :: _,_)
=> Full(Supplier.findAll.toList)
... and I get the compilation-error:
"could not find implicit value for
parameter
cvt:com.mycompany.api.RestApi.JxCvtPF[ScalaObject]"
Can someone please give me an example of how to modify the cvt-method to be able to convert list of convertables to a LiftResponse?
Cheers!
/J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了 cvt 方法的解决方案。将类型更改为隐式 def cvt: JxCvtPF[Any] 以能够匹配特定类。然后按如下方式实现可转换列表:
完整解决方案:
}
I found a solution to the cvt-method. Change the type to implicit def cvt: JxCvtPF[Any] to be able to match specific classes. Then implement the list of convertables as such:
Full solution:
}