使用 json4s 处理 POST 请求中的 JSON 数据

Handle JSON data in POST request using json4s

我正在尝试将 JSON i/p 映射到我的案例 class CacheRequest。
请求是 POST.
我是 Scala 和 Akka 的新手。

import org.json4s.{DefaultFormats, Formats}
implicit val formats: Formats = DefaultFormats
val route: Route = traceContextAwareRoute {
            pathPrefix(system.name) {
              post {
                path("sample") {
                  entity(as[CacheRequest]) { x => {
                    val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f)

                    onComplete(getSystemStock(cacheRequest)) {
                      (response: Try[Option[CacheResponse]]) => complete(processResponse(response))
                    }
                  }

                  }
                }
              }
            }


我的情况class是这样的

case class CacheRequest(a: String,
                        b: String,
                        c: Int,
                        d: Int,
                        e: Int,
                        f: Int)

收到类似

的错误
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest]

not enough arguments for method as: (implicit um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest])akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[mcc.movie.common.model.CacheRequest]

我应该使用 json4s 来做到这一点。
关于这方面的任何帮助对我来说都很好。

您可以使用这个库:https://github.com/hseeberger/akka-http-json

代码可能是这样的

import org.json4s.{DefaultFormats, Formats, jackson}
import de.heikoseeberger.akkahttpjson4s.Json4sSupport

class Route {
  import Json4sSupport._
  implicit val formats: Formats = DefaultFormats
  implicit val serialization = jackson.Serialization

  val route: Route = traceContextAwareRoute {
    pathPrefix(system.name) {
      post {
        path("sample") {
          entity(as[CacheRequest]) { x => {
            val cacheRequest: CacheRequest = CacheRequest(x.a, x.b, x.c, x.d, x.e, x.f)

            onComplete(getSystemStock(cacheRequest)) {
              (response: Try[Option[CacheResponse]]) => complete(processResponse(response))
            }
          }
        }
      }
    }
  }
}