斯卡拉电梯 + REST - 使用serveJx 返回数组

发布于 2024-10-24 08:46:16 字数 1364 浏览 1 评论 0原文

我有一个类扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2024-10-31 08:46:16

我找到了 cvt 方法的解决方案。将类型更改为隐式 def cvt: JxCvtPF[Any] 以能够匹配特定类。然后按如下方式实现可转换列表:

//Generic list templates
    case (JsonSelect, c : List[Convertable], _) => JArray(for{item <- c} yield item.toJson)
    case (XmlSelect, c : List[Convertable], _) => <list>{c.mapConserve(f => f.toXml)}</list>

完整解决方案:

object Rest  extends RestHelper {
implicit def cvt: JxCvtPF[Any] = {

    //Generic list templates
    case (JsonSelect, c : List[Convertable], _) => JArray(for{item <- c} yield item.toJson)
    case (XmlSelect, c : List[Convertable], _) => <list>{c.mapConserve(f => f.toXml)}</list>

    //Single-items of convertable
    case (JsonSelect, c : Convertable, _) => c.toJson
    case (XmlSelect, c : Convertable, _) => c.toXml
}
serveJx {

    case "supplier" :: "findAll" :: _ Get _
        => Full(Supplier.findAll)

    case "channelPackage" :: "findAll" :: _ Get _
        => Full(ChannelPackage.findAll)

    case "channelPackage" :: "findFromSupplierId" :: supplierId :: _ Get _
        => Full(Supplier.find(supplierId).head.channelPackages)

    case "device" :: "findAll" :: _ Get _
        => Full(Device.findAll)

    case "device" :: "findFromSupplierId" :: supplierId :: _ Get _
        => Full(Supplier.find(supplierId).head.devices)
}

}

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:

//Generic list templates
    case (JsonSelect, c : List[Convertable], _) => JArray(for{item <- c} yield item.toJson)
    case (XmlSelect, c : List[Convertable], _) => <list>{c.mapConserve(f => f.toXml)}</list>

Full solution:

object Rest  extends RestHelper {
implicit def cvt: JxCvtPF[Any] = {

    //Generic list templates
    case (JsonSelect, c : List[Convertable], _) => JArray(for{item <- c} yield item.toJson)
    case (XmlSelect, c : List[Convertable], _) => <list>{c.mapConserve(f => f.toXml)}</list>

    //Single-items of convertable
    case (JsonSelect, c : Convertable, _) => c.toJson
    case (XmlSelect, c : Convertable, _) => c.toXml
}
serveJx {

    case "supplier" :: "findAll" :: _ Get _
        => Full(Supplier.findAll)

    case "channelPackage" :: "findAll" :: _ Get _
        => Full(ChannelPackage.findAll)

    case "channelPackage" :: "findFromSupplierId" :: supplierId :: _ Get _
        => Full(Supplier.find(supplierId).head.channelPackages)

    case "device" :: "findAll" :: _ Get _
        => Full(Device.findAll)

    case "device" :: "findFromSupplierId" :: supplierId :: _ Get _
        => Full(Supplier.find(supplierId).head.devices)
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文