简体   繁体   English

找不到参数FromRequestUnmarshaller Akka的隐式值

[英]could not find implicit value for parameter FromRequestUnmarshaller Akka

I am trying to create this api for objects like user, location, and visits. 我正在尝试为用户,位置和访问之类的对象创建此api。 And I want to have post methods for adding and updating values of these objects. 我想拥有用于添加和更新这些对象的值的后期方法。 However, I do not know how to pass the object to the route of an api. 但是,我不知道如何将对象传递给api的路由。 A part of my route for the location update: 我的位置更新路线的一部分:

trait ApiRoute extends MyDatabases with FailFastCirceSupport {
     val routes =
          pathPrefix("locations") {
            pathSingleSlash {
              pathPrefix(LongNumber) { id =>
                  post { entity(as[Location]){
                    location => {
                      onSuccess(locationRepository.update(location)) {
                        result => complete(result.asJson)
                      }
                    }
                  }
}

However, when I try to build update in such a way I get an error: 但是,当我尝试以这种方式构建更新时,出现错误:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[models.Location]
[error]               post { entity(as[Location]){

Json encoder for the location: Json编码器的位置:

package object application {
      implicit val locationEncoder = new Encoder[Location] {
        final def apply(location: Location): Json = Json.obj(
          ("id", Json.fromLong(location.id)),
          ("place", Json.fromString(location.place)),
          ("country", Json.fromString(location.country)),
          ("city", Json.fromString(location.city)),
          ("distance", Json.fromLong(location.distance))
        )
      }

I am using Slick to model and get all the data from the database: 我正在使用Slick建模并从数据库中获取所有数据:

case class Location (id: Long, place: String, country: String, city: String, distance: Long)

class LocationTable(tag: Tag) extends Table[Location](tag, "locations") {
  val id = column[Long]("id", O.PrimaryKey)
  val place = column[String]("place")
  val country = column[String]("country")
  val city = column[String]("city")
  val distance = column[Long]("distance")

  def * = (id, place, country, city, distance) <> (Location.apply _ tupled, Location.unapply)
}

object LocationTable {
  val table = TableQuery[LocationTable]
}

class LocationRepository(db: Database) {
  val locationTableQuery = TableQuery[LocationTable]

  def create(location: Location): Future[Location] =
    db.run(locationTableQuery returning locationTableQuery += location)

  def update(location: Location): Future[Int] =
    db.run(locationTableQuery.filter(_.id === location.id).update(location))
}

So what should I add or change in my code to get rid of the exception and make it work? 那么我应该在代码中添加或更改什么以摆脱异常并使之正常工作呢?

If you are adding or updating a Location, that Location needs a Decoder as well to read the serialized data from the client that comes across the wire as the HTTP entity. 如果要添加或更新位置,则该位置也需要一个解码器,以从作为HTTP实体的网络客户端读取序列化的数据。 Akka HTTP also needs a FromRequestUnmarshaller in conjunction with the Decoder to decode the request entity which in this example is the Location you want to add or update, and the one that you extract the id from. Akka HTTP还需要将FromRequestUnmarshaller与Decoder结合使用,以对请求实体进行解码,在该示例中,该请求实体是您要添加或更新的位置,以及从中提取ID的位置。

Using Scala's Circe library for JSON handling, then the Akka HTTP JSON project has part of what you need . 使用Scala的Circe库进行JSON处理,那么Akka HTTP JSON项目就可以满足您的需要 As that project indicates, add the following to your build.sbt 如该项目所示,将以下内容添加到build.sbt中

// All releases including intermediate ones are published here,
// final ones are also published to Maven Central.
resolvers += Resolver.bintrayRepo("hseeberger", "maven")

libraryDependencies ++= List(
  "de.heikoseeberger" %% "akka-http-circe" % "1.18.0"
)

and then you can mix in the support you need using FailFastCirceSupport or ErrorAccumulatingCirceSupport. 然后可以使用FailFastCirceSupport或ErrorAccumulatingCirceSupport混合所需的支持。 Add that to the class that defines your routes with 将其添加到定义您的路线的类中

class SomeClassWithRoutes extends ErrorAccumulatingCirceSupport

or 要么

class SomeClassWithRoutes extends FailFastCirceSupport

depending on whether you want to fail on the first error, if any, or accumulate them. 取决于您是想在第一个错误(如果有)上还是失败还是累积它们。

You still need to have a Decoder[Location] in scope as well. 您仍然还需要在范围内具有Decoder [Location]。 For that you can see Circe's documentation , but one quick way to define that if you want the default field names is to use the following imports in your route definition class or file so that Circe creates the necessary Decoder for you. 为此,您可以参阅Circe的文档 ,但是一种定义默认字段名称的快速方法是在路由定义类或文件中使用以下导入,以便Circe为您创建必要的Decoder。

import io.circe.generic.auto._
import io.circe.Decoder

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

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