akka-http错误:无法找到参数um的隐式值:akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller

问题描述 投票:2回答:3

我知道已经有人问过,但我似乎无法找到答案。这是我的代码:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol

final case class Client(clientId:Int, clientName:String, platformIds:Int, host:String, password:String)

object ClientJson extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val clientFormat = jsonFormat5(Client)
}

class HTTPListenerActor extends Actor with ImplicitMaterializer with RoadMap {

implicit val conf = context.system.settings.config
implicit val system = context.system
implicit val ec = context.dispatcher


Await.result(Http().bindAndHandle(roads, "interface", 8080), Duration.Inf)

override def receive:Receive = Actor.emptyBehavior
}

trait RoadMap extends Directives  {

val roads: Route = path("client"/IntNumber) { id =>
    import ClientJson._
    post {
        entity(as[Client]) { c => complete {c} }
    }
  }
}

此代码生成错误

 [ant:scalac] /Users/smalov/Workspace/api-service/src/main/scala/com/acheron/HTTPListenerActor.scala:51: error: could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[com.acheron.Client]
 [ant:scalac]           entity(as[Client]) { c =>

现在造成这种错误的最常见原因是忘记将编组隐式导入到roads定义附近的范围内,但是,我没有忘记这一点。另一个原因可能是我在范围而不是FlowMaterializer隐含ActorMaterializer,但ImplictMaterializer特质负责这一点。

还有什么我可能会失踪的?

我正在使用Scala 2.11.7,Akka 2.3.11,akka-http 1.0,spray-json 1.3.2

spray-json akka-http
3个回答
10
投票

我也得到了同样的错误,导入后解决了

“akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._”

可能这会有所帮助


3
投票

看来我需要在ActorMaterializer特征范围内的RoadMap。因此,添加implicit val materializer: ActorMaterializer解决了编译问题。

我希望这个错误更具描述性......


2
投票

导入这些东西。

import spray.json.RootJsonFormat
import spray.json.DefaultJsonProtocol._

当你调用entity(as[Client])时,请在范围内

implicit val clientJsonFormat: RootJsonFormat[Client] = jsonFormat5(Client)
© www.soinside.com 2019 - 2024. All rights reserved.