我知道已经有人问过了,但我似乎找不到答案。下面是我的代码:
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
发布于 2016-12-16 06:27:44
我也遇到了同样的错误,在导入后解决了
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._也许这会有帮助
发布于 2018-04-21 15:19:49
导入这些东西。
import spray.json.RootJsonFormat
import spray.json.DefaultJsonProtocol._然后在调用entity(as[Client])时将其放在作用域中
implicit val clientJsonFormat: RootJsonFormat[Client] = jsonFormat5(Client)让它在范围内的传统方法是将这些放在一个性状中,例如
trait MyJSON extends SprayJsonSupport with DefaultJsonProtocol with NullOptions {
implicit val clientJsonFormat: RootJsonFormat[Client] = jsonFormat5(Client)
}然后,将其混合到包含web路由的对象中,如下所示:
object MyWeb extends MyJSON {
val myRoute =
get {
path("client") {
complete(Future successful Client(...))
}
} ~ post {
path("client") {
entity(as[Client]) { client =>
...
}
}
}
}发布于 2015-11-12 12:32:23
在RoadMap特征中,我似乎需要范围内的ActorMaterializer。因此,在其中添加implicit val materializer: ActorMaterializer解决了编译问题。
我希望这个错误更具描述性……
https://stackoverflow.com/questions/33663732
复制相似问题