1

我知道这个问题已经被问过很多次了,但是他们都使用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.scalaContact中定义的数据模型。

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

4

1 回答 1

3

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

于 2016-04-20T20:16:30.367 回答