斯卡拉 – 喷雾/阿卡失踪隐含
MyService.
scala:33:找不到参数eh的隐式值:spray.routing.ExceptionHandler
我在使用Akka时遇到了“缺少隐式”编译错误,在spray.io代码中,对一个单独的后端服务器进行http调用,作为响应http get的一部分.代码需要导入相当多的Spray和Akka库,因此有点难以确定是否存在导致此问题的库冲突,我宁愿想出如何在这个和其他情况下逻辑地跟踪这类问题. 调用runRoute(myRoute)时遇到缺少的隐式 这是代码: import spray.routing._ import akka.actor.Actor import akka.actor.ActorSystem import spray.http._ import MediaTypes._ import akka.io.IO import spray.httpx.RequestBuilding._ import scala.concurrent.Future import spray.can.Http import spray.http._ import akka.util.Timeout import HttpMethods._ import akka.pattern.ask import akka.event.Logging import scala.concurrent.duration._ // we don't implement our route structure directly in the service actor because // we want to be able to test it independently,without having to spin up an actor class MyServiceActor extends Actor with MyService with akka.actor.ActorLogging { log.info("Starting") // the HttpService trait defines only one abstract member,which // connects the services environment to the enclosing actor or test def actorRefFactory = context // this actor only runs our route,but you could add // other things here,like request stream processing // or timeout handling def receive = runRoute(myRoute) } // this trait defines our service behavior independently from the service actor trait MyService extends HttpService { implicit val system: ActorSystem = ActorSystem() implicit val timeout: Timeout = Timeout(15.seconds) import system.dispatcher // implicit execution context //val logger = context.actorSelection("/user/logger") val logger = actorRefFactory.actorSelection("../logger") val myRoute = { def forward(): String = { logger ! Log("forwarding to backend") val response: Future[HttpResponse] = (IO(Http) ? Get("http:3080//localhost/backend")).mapTo[HttpResponse] "<html><body><h1>api response after backend processing</h1></body></html>" } path("") { get { respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default,so we simply override here complete(forward) } } } } } 我想知道解决这个问题的最佳方法是什么,希望能够提供有关如何解决类似问题的洞察力,因为它们在某种程度上并不能直接追踪. 编辑:当试图直接传递implicits,如下面的@ christian的答案,我得到: MyService.scala:35:含糊不清的隐含值: 不太确定为什么具体如@ christian的答案为编译器留下了歧义的空间…… 解决方法
runRoute期待一些暗示.您缺少导入:
import spray.routing.RejectionHandler.Default 更新: runRoute(route)(ExceptionHandler.default,RejectionHandler.Default,context,RoutingSettings.default,LoggingContext.fromActorRefFactory) UPDATE2: import akka.actor.Actor import akka.io.IO import spray.httpx.RequestBuilding._ import spray.http.MediaTypes._ import spray.routing.{RoutingSettings,RejectionHandler,ExceptionHandler,HttpService} import spray.util.LoggingContext import scala.concurrent.Future import spray.can.Http import spray.http._ import akka.util.Timeout import HttpMethods._ import akka.pattern.ask import akka.event.Logging import scala.concurrent.duration._ // we don't implement our route structure directly in the service actor because // we want to be able to test it independently,which // connects the services environment to the enclosing actor or test implicit def actorRefFactory = context // this actor only runs our route,like request stream processing // or timeout handling def receive = runRoute(myRoute)(ExceptionHandler.default,LoggingContext.fromActorRefFactory) } // this trait defines our service behavior independently from the service actor trait MyService extends HttpService { this: MyServiceActor => implicit val timeout: Timeout = Timeout(15.seconds) implicit val system = context.system //val logger = context.actorSelection("/user/logger") val logger = actorRefFactory.actorSelection("../logger") val myRoute = { def forward(): String = { //logger ! Log("forwarding to backend") val response: Future[HttpResponse] = (IO(Http) ? Get("http:3080//localhost/backend")).mapTo[HttpResponse] "<html><body><h1>api response after backend processing</h1></body></html>" } path("") { get { respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default,so we simply override here complete(forward) } } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |