scala - Spray/Akka 缺失隐式

标签 scala akka spray

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/

相关文章:

scala - 如何告诉 sbt-proguard 包含 java *.jars?

scala - 创建时用 for 循环填充不可变映射

json - 如何根据丰富进行动态调度?

regex - 如何将一个字符串与另一个之间有空格的字符串进行比较

scala - 对 DStream 进行类型参数化

scala - 使用testkit对akka进行gradle测试

java - 是否可以在 Android 项目中运行最新版本的 Akka 框架(2.5.2)?

java - JUnit 在 play 框架 2.0.4 中测试 Akka 参与者 - scala.concurrent.duration.Duration 上的 JAVA ClassNotFoundException

scala - 如何使用 Spray 创建带有表单字段内容的 POST 请求?

scala - 隐式超出 Spray 示例代码 : what's happening here? 的范围