更新到spray 1.2后,我遇到了一个与我的
JSON编程器完美配合的问题.在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 }
所以“马歇尔故事”本身似乎是隐含的范围.我现在很困惑,因为我从来没有做任何其他事情能够组织未来的期货.
解决方法
好的,解决方案很容易,但很难找到,因为没有错误信息指向它:
您需要在范围中指定一个隐式执行上下文,以便能够使用也隐式的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 }
喷雾1.1,Scala 2.10.0和akka 2.1不是这样