akka 无法找到参数um的隐式值:解组处理,FromRequestUnmarshaller[类]

uurity8g  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(125)

我是akka http的新手,我在编组和解组case类时遇到了困难,这是我的代码

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]
m0rkklqb

m0rkklqb1#

有一个简单的方法可以解决这个问题。akka-http-Jackson有一个请求解组器的实现。
sbt添加库:

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

然后在代码中

import de.heikoseeberger.akkahttpjackson.JacksonSupport._
nwo49xxi

nwo49xxi2#

默认情况下,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中提供了详细步骤
希望对你有帮助!!

相关问题