我知道这个问题之前已被多次询问过,但是所有这些都使用了spray-json。我想用play-json。这可能就是提出的解决方案无法解决问题的原因。
route
的特征位于名为RestApi.scala的单独文件中。使用它的Http.bindAndHandle
位于名为Main.scala的文件中。删除不相关元素的两个文件如下所示。如果您想查看整个文件,请单击上面的链接。
RestApi.scala
package restApi
import akka.actor.{ActorSystem, Props}
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.pattern.ask
import akka.stream.ActorMaterializer
import akka.util.Timeout
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
trait RestApi {
import models._
import cassandraDB.{WriterActor, ReaderActor}
implicit val system: ActorSystem
implicit val materializer: ActorMaterializer
implicit val ec: ExecutionContext
implicit val timeout = Timeout(20 seconds)
val cassandraWriterWorker = system.actorOf(Props[WriterActor], "cassandra-writer-actor")
val cassandraReaderWorker = system.actorOf(Props[ReaderActor], "cassandra-reader-actor")
val route =
pathPrefix("api") {
pathSuffix("contact") {
// the line below has the error
(post & entity(as[Contact])) { contact =>
complete {
cassandraWriterWorker ! contact
StatusCodes.OK
}
}
} ~
pathSuffix("gps"/ "log") {
// an analogous error message is shown in the line below
(post & entity(as[GpsLog])) { gpsLog =>
complete {
cassandraWriterWorker ! gpsLog
StatusCodes.OK
}
}
}
}
}
Main.scala
package initialization
import akka.actor.{ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import cassandraDB.{ConfigCassandraCluster, ReaderActor, WriterActor}
import gcm.GcmServer
import restApi.RestApi
object Main extends App with ConfigCassandraCluster with RestApi {
override implicit val system = ActorSystem()
override implicit val materializer = ActorMaterializer()
override implicit val ec = system.dispatcher
val write = system.actorOf(Props(new WriterActor(cluster)))
val read = system.actorOf(Props(new ReaderActor(cluster)))
val gcmServer = system.actorOf(Props(new GcmServer(11054...,"AIzaSyCOnVK...")), "gcm-server")
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
}
我指出了RestApi.scala中发生错误的行 错误消息:
Error:(31, 26) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[models.Contact]
(post & entity(as[Contact])) { contact =>
^
Contact
是Contact.scala中定义的数据模型。
我希望这不是重复,但我真的不这么认为。非常感谢任何帮助。
答案 0 :(得分:2)
事实证明,有一个简单的解决方案。只需将resolvers += Resolver.bintrayRepo("hseeberger", "maven")
添加到您的构建文件,然后"de.heikoseeberger" %% "akka-http-play-json" % "1.5.3"
添加到您的libraryDependencies并将import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._
导入您的代码。你现在可以像使用spray-json一样使用play-json。