9

更新到 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 似乎在隐式范围内。我现在很困惑,因为我以前从来不需要做任何其他事情来编组期货。

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

4

1 回答 1

18

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

您需要在范围内指定一个隐式执行上下文才能使用同样隐式的 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 并非如此

于 2013-11-06T18:46:56.977 回答