【问题标题】:spray Marshaller for futures not in implicit scope after upgrading to spray 1.2升级到喷雾 1.2 后,喷雾 Marshaller 不在隐含范围内
【发布时间】:2013-11-06 10:45:08
【问题描述】:

更新到 spray 1.2 后,我遇到了一个与 1.1 完美配合的 JSON-Marshallers 的问题。在 HttpService 中执行以下操作

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol{ self : ActorLogging =>
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Future[Test]]) = 17
    def hallo = test 
}

导致以下错误:

could not find implicit value for parameter marshaller:
spray.httpx.marshalling.Marshaller[scala.concurrent.Future[amanuensis.story.Story]]

当我删除未来时,一切正常:

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol { self : ActorLogging =>
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Test]) = 17
    def hallo = test

}

所以 Story 本身的 Marshaller 似乎在隐式范围内。我现在很困惑,因为我以前从来不需要做任何其他事情来编组期货。

我真的很感激一个提示,我在这里做错了什么......

【问题讨论】:

  • 版喷版是这样的吗?
  • Spray-Version 是 1.2-RC2,但当前的夜间版本也是如此。 Akka 是 2.2.3,Scala 是 2.10.3。
  • 刚刚添加了case类定义和JsonFormat的定义...

标签: scala akka future spray


【解决方案1】:

好的,解决方案很容易但很难找到,因为没有指向它的错误消息:

您需要在范围内指定一个隐式执行上下文才能使用同样隐式的 Marshaller[Future[...]]。就我而言:

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol{ self : ActorLogging =>
    //the following line was missing
    implicit def executionContext = actorRefFactory.dispatcher
    //
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Future[Test]]) = 17
    def hallo = test 
}

spray 1.1、Scala 2.10.0 和 akka 2.1 并非如此

【讨论】:

  • 行为在 1.1-RC2 和 1.2-RC2 中真的不同吗?
  • 我刚试了一下,1.1-RC2和1.2-RC2也是一样。但是,您的其余分析是正确的。是的,scala 编译器没有为嵌套的隐式提供更好的错误消息确实很痛苦,但我们目前对此无能为力。
  • 天哪,谢谢。我花了 7 个小时来解决这个问题 - 编译器抱怨我传递的是 Future[String] 而不是 ToResponseMarshallable,并且没有提示缺少隐式导入。导入 context.dispatcher 有帮助!
猜你喜欢
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多