找不到参数 um 的隐式值:akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[class]

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[class]

我是 akka http 的新手,我在编组和取消编组我的案例时遇到困难 class 这是我的代码

case class Event(uuid:String)

//main class 
class demo {

    val route: Route =

    post {
            path("create-event") {
              entity(as[Event]) { event =>
                  complete("event created")
                }
              }
            }
          }
    }

我在这一行遇到编译时错误

entity(as[Event]) { event =>

 could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[models.event.Event]            

有一个简单的方法可以解决这个问题。 akka-http-jackson 有一个 Request Unmarshaller 的实现。

sbt 添加库:

"de.heikoseeberger"                         %% "akka-http-jackson"             % "1.27.0"

然后在您的代码中

import de.heikoseeberger.akkahttpjackson.JacksonSupport._

默认情况下,Akka-Http 使用 spray 来编组和解组json,错误是因为没有定义用于转换的隐式转换器。

import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

trait EventProtocol extends DefaultJsonProtocol {
  implicit val eventJsonFormat = jsonFormat1(Event)
}

class demo extends SprayJsonSupport with EventProtocol {
// your code
}

akka-http中提供了详细步骤documentation

希望对您有所帮助!!