akka-http:找不到参数解组的隐式值 [英] akka-http : could not find implicit value for parameter unmarshalling

查看:156
本文介绍了akka-http:找不到参数解组的隐式值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spray json支持看起来像这样

My spray json support looks like this

object MarshallingSupport extends SprayJsonSupport {
  implicit def json4sFormats: Formats = DefaultFormats
}

在我的路线中,我想将请求映射到dto

And in my route I want to map the request to a dto

object Main extends App with AppConfig with BaseService with MainActorSystem {

  val processor = system.actorOf(Props(), "processorActor")
  val view = system.actorOf(Props(), "processorActor")

  override protected implicit val executor: ExecutionContext = system.dispatcher
  override protected val log: LoggingAdapter = Logging(system, getClass)
  override protected implicit val materializer: ActorMaterializer = ActorMaterializer()

  Http().bindAndHandle(routes(processor, view), httpInterface, httpPort)
}

trait BaseServiceRoute {
  protected implicit def executor: ExecutionContext
  protected implicit def materializer: ActorMaterializer
  protected def log: LoggingAdapter
}

trait MainActorSystem {
  implicit val system = ActorSystem("booking")
}

final case class CalculatePriceForRangeDto(unitId: Int, from: Long, to: Long)

trait PriceServiceRoute extends BaseServiceRoute {

  implicit val timeout = Timeout(30 seconds)

  import com.example.crudapi.utils.MarshallingSupport._

  def customersRoute(command: ActorRef, query: ActorRef) = pathPrefix("price") {
    post {
      path("calculate") {
        decodeRequest {
          entity(as[CalculatePriceForRangeDto]) {
            priceForRange => onComplete((query ? CalculatePriceForRange(

但我得到了

Error:(32, 20) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[com.example.crudapi.http.routes.CalculatePriceForRangeDto]
      entity(as[CalculatePriceForRangeDto]) {
               ^

已看到所有相关的SO问题,但没有任何解决我的问题的部分。奇怪的是,我尝试了Typesafe模板akka-dddd-cqrs,并且可以正常工作,使用相同的代码。

Have seen all related SO questions but nothing solved my issue. Strange part is that I tried Typesafe template akka-dddd-cqrs and it works, same code.

我错过了一些东西吗?带有隐式上下文?
有什么想法吗?

Am I missing something with implicit context? Any ideas of what could it be?

推荐答案

SprayJsonSupport 与spray-json(不适用于json4s)一起使用,因此您需要按以下方式定义封送处理程序

SprayJsonSupport works with spray-json (not with json4s). Thus you need to defined marshallers as follows

trait JsonMarshallers extends DefaultJsonProtocol {
  implicit val DigestItemWireFormat = jsonFormat6(DigestItemWire.apply)

  implicit val LocalDateTimeFormat = new JsonFormat[LocalDateTime] {

    private val iso_date_time = DateTimeFormatter.ISO_DATE_TIME

    def write(x: LocalDateTime) = JsString(iso_date_time.format(x))

    def read(value: JsValue) = value match {
      case JsString(x) => LocalDateTime.parse(x, iso_date_time)
      case x => throw new RuntimeException(s"Unexpected type %s on parsing of LocalDateTime type".format(x.getClass.getName))
    }
  }
}

,然后在使用或混合使用的范围内导入 JsonMarshallers ._ 使用 PriceServiceRoute 并在文件的开头导入 akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport ._

and then import JsonMarshallers._ in the scope where you use them or mix it in with PriceServiceRoute and import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ at the beginning of the file.

这篇关于akka-http:找不到参数解组的隐式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆