play-json - 找不到参数 um 的隐式值

play-json - Could not find implicit value for parameter um

我知道这个问题已经被问过很多次了,但是他们都用spray-json。我想使用 play-json。这可能就是建议的解决方案不能解决问题的原因。 具有 route 的特征位于名为 RestApi.scala. The Http.bindAndHandle which makes use of it is in the file named 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中定义的数据模型。

我希望这不是重复的,但我真的不这么认为。非常感谢任何帮助。

原来有一个简单的解决方案。只需将 resolvers += Resolver.bintrayRepo("hseeberger", "maven") 添加到您的构建文件,然后将 "de.heikoseeberger" %% "akka-http-play-json" % "1.5.3" 添加到您的 libraryDependencies 并将 import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._ 导入您的代码。您现在可以像使用 spray-json.

一样使用 play-json