我是akka http的新手,在整理和解开案例类时遇到困难这是我的代码
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使用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中提供
希望有帮助!
有一个简单的方法可以解决此问题。 akka-http-jackson有一个Request Unmarshaller的实现。
sbt添加lib:
"de.heikoseeberger" %% "akka-http-jackson" % "1.27.0"
然后在您的代码中
import de.heikoseeberger.akkahttpjackson.JacksonSupport._