Ham*_*amy 1 scala akka spray spray-json spray-dsl
此代码导致编译错误could not find implicit value for parameter marshaller:
spray.httpx.marshalling.ToResponseMarshaller[List[akka.actor.ActorRef]].
我不认为问题是ActorRef,因为改变它.mapTo[List[String]]显示相同的编译错误
一般来说,喷雾如何编组与所有暗示有点混淆 - 有没有办法使这个明确,例如ListProtocol.marshal(value)?
import akka.actor.Actor
import spray.http.HttpResponse
import spray.http.HttpRequest
import spray.http.Uri
import spray.http._
import spray.routing._
import HttpMethods._
import akka.actor.ActorRef
import akka.pattern.ask
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Success
import scala.util.Failure
import spray.http.StatusCodes.InternalServerError
import spray.json.DefaultJsonProtocol
import spray.httpx.SprayJsonSupport._
import spray.httpx.marshalling._
import spray.http._
class HttpApi(val manager: ActorRef) extends HttpServiceActor {
def receive = runRoute {
path("nodes") {
get {
onComplete(manager.ask(NodeList())(3.seconds).mapTo[List[ActorRef]]) {
case Success(value) => {
// Compile error happens here
complete(value)
}
case Failure(ex) => {
complete(InternalServerError, s"An error occurred: ${ex.getMessage}")
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更改此导入
import spray.json.DefaultJsonProtocol
Run Code Online (Sandbox Code Playgroud)
至
import spray.json.DefaultJsonProtocol._
Run Code Online (Sandbox Code Playgroud)
也就是说,您要导入该对象中定义的implicits,而不是对象本身.
或者,您可以扩展特征以获取隐含:
class HttpApi(val manager: ActorRef) extends HttpServiceActor
with DefaultJsonProtocol {
Run Code Online (Sandbox Code Playgroud)