简体   繁体   English

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

[英]play-json - Could not find implicit value for parameter um

I know this question has been asked many times before, however all of them use spray-json. 我知道这个问题已经被问过很多次了,但是他们都使用spray-json。 I wanted to use play-json. 我想使用play-json。 This might be why the proposed solutions don't solve the problem. 这可能就是为什么建议的解决方案无法解决问题的原因。 The trait with the route is in a separate file named RestApi.scala . 带有route的特征位于名为RestApi.scala的单独文件中。 The Http.bindAndHandle which makes use of it is in the file named Main.scala . 使用它的Http.bindAndHandle在名为Main.scala的文件中。 Both files without irrelevant element removed are presented below. 下面显示了两个没有删除无关元素的文件。 If you want to see the whole file please click at the links above. 如果您想查看整个文件,请单击上面的链接。

RestApi.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 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)


}

I point out the line where the error occurs in RestApi.scala Error Message: 我指出在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 =>
                         ^

The Contact is a data model defined in Contact.scala . ContactContact.scala中定义的数据模型。

I hope this isn't a duplicate, but I really don't think so. 我希望这不是重复的,但我确实不这么认为。 Any help is much appreciated. 任何帮助深表感谢。

It turns out there is an easy solution. 事实证明,有一个简单的解决方案。 Simply add resolvers += Resolver.bintrayRepo("hseeberger", "maven") to your build file and then "de.heikoseeberger" %% "akka-http-play-json" % "1.5.3" to your libraryDependencies and import import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._ into your code. 只需将resolvers += Resolver.bintrayRepo("hseeberger", "maven")到您的构建文件中,然后将"de.heikoseeberger" %% "akka-http-play-json" % "1.5.3"到您的libraryDependencies并导入import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._到您的代码中。 You can now use play-json like you would use spray-json. 您现在可以像使用spray-json一样使用play-json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM