gpt4 book ai didi

scala - Spray/Akka 缺失隐式

转载 作者:行者123 更新时间:2023-12-02 12:22:47 27 4
gpt4 key购买 nike

MyService.scala:33: could not find implicit value for parameter 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)
}
}
}
}
}

我想知道解决这个问题的最佳方法是什么,希望能够深入了解如何解决缺少隐含项的类似问题,因为它们本质上并不容易追踪。

编辑:当尝试像下面@christian的答案一样直接传递隐式时,我得到:

MyService.scala:35: ambiguous implicit values:
both value context in trait Actor of type => akka.actor.ActorContext
and value system in trait MyService of type => akka.actor.ActorSystem
match expected type akka.actor.ActorRefFactory<br/>
RoutingSettings.default, LoggingContext.fromActorRefFactory)
^

不太清楚为什么像 @christian 的回答那样具体会给编译器留下歧义的空间......

最佳答案

今天早些时候我遇到了同样的“无法找到参数的隐式值:spray.routing.ExceptionHandler”错误。我尝试了 @Christian 的方法,但看到一些“xxx 的隐式值”正在逐渐增加。在稍微侦查了错误消息之后,我发现将implicit val system = context.system添加到runRoute解决了问题的actor中。

关于scala - Spray/Akka 缺失隐式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24284641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com