【问题标题】:Spray marshalling collection of ToResponseMarshallableToResponseMarshallable 的喷雾编组集合
【发布时间】:2015-04-16 07:59:15
【问题描述】:

我有一个简单的喷涂场景,但它并没有像我预期的那样工作。

我有一个喷雾服务器,可以将工作分派到不同的模块,然后返回它们的组合响应。

由于我不想限制模块响应,因此我选择了模块的方法返回 ToResponseMarshallable,因为我有需要返回纯字符串的模块。

模块签名可以是:

def moduleProcessing(): ToResponseMarshallable = randomString()

我的“完整”块看起来与此类似:

complete {   

    val response1 = moduleProcessing()
    val response2 = moduleProcessing()
    Seq(response1,response2) 
}

在前面的例子中,我希望得到:

[{"someRandomString"},{"anotherRandomString"}]

但我得到了:

[{},{}]

当然,如果我返回单个响应或者如果我将 moduleProcessing 返回类型的签名更改为任何 Marshallable 类型,它将按预期传播。

提前感谢您的帮助!

【问题讨论】:

    标签: scala collections response marshalling spray


    【解决方案1】:

    我觉得你的moduleProcessing 方法直接返回ToResponseMarshallable[T] 很奇怪。 spray 方式是让此方法返回 T 并在您通过提供 T 完成请求时具有范围内的 Marshaller[T]

    最后,我不认为Marshaller.of[Seq[T]]built in marshaller in spray

    这意味着您需要提供自己的Seq marshaller。这可以将 seq 中每个项目的实际编组委托给隐式 Marshaller[T] 并连接结果。 Many builtin spray marshallers 需要像这样的隐式 Marshaller[T] 值。或者,如果您想要一个简单的 Seq[T] 来串列 marshaller,请尝试以下操作:

    implicit val CustomMarshaller = Marshaller.delegate[Seq[T], String](`text/html`)(_.mkString("{", ",", "}"))
    

    【讨论】:

      猜你喜欢
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多