如何调试akka http的route dsl

How to debug akka http's route dsl

我正在尝试为 akka http 创建一个解组器,从 avro 到自定义案例 class。但它给了我一个非常模糊的错误:"could not find implicit value"。我该如何调试它或让 scala 提示我问题出在哪里?

我是这样设置路由的:

class MetricsRoute(implicit val system: ActorSystem, implicit val materializer: ActorMaterializer) {
import system.dispatcher

  def getRoute() = {
    path("metrics") {
      put {
        decodeRequest {
          entity(as[Metrics]) { metrics: Metrics =>
            println(metrics.time)
            complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>hi!</h1>"))
          }
        }
      }
    }
  }

在同一个 class 中,我还创建了这样的解组器:

  implicit def avroUnmarshaller(): FromRequestUnmarshaller[Metrics] =
    Unmarshaller.withMaterializer {
      implicit ex: ExecutionContext =>
      implicit mat: Materializer =>
        request: HttpRequest => {
          val inputStream = request.entity.dataBytes.runWith(
            StreamConverters.asInputStream(FiniteDuration(3, TimeUnit.SECONDS))
          )

          val reader = new SpecificDatumReader[AvroMetrics](classOf[AvroMetrics])
          val decoder:BinaryDecoder = DecoderFactory.get().binaryDecoder(inputStream, null)

          //AvroMetrics is a case class generated from the avro schema
          val avroMetrics:AvroMetrics = AvroMetrics(0, 0, List())
          reader.read(avroMetrics, decoder)

          Future {
            //converts the avro case class to the case class specific for my application
            convertMetrics(avroMetrics)
          }
        }
  }

但这给了我非常模糊的 'could not find implicit value' 错误:

[error] /mypath/MetricsRoute.scala:34: could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[my.package.types.Metrics]
[error]           entity(as[Metrics]) { metrics: Metrics =>
[error]                    ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

我该如何着手调试缺失的内容或我做错了什么?

编辑:

值得注意的是,当我自己指定解组程序时它确实有效,因此更改

entity(as[Metrics]) { metrics: Metrics =>

entity(avroUnmarshaller) { metrics: Metrics =>

这似乎表明编组器代码本身没有错,但我对类型做错了什么?

编译器正在寻找一个FromRequestUnmarshaller[Metrics],你定义的类型是() => FromRequestUnmarshaller[Metrics]

尝试定义没有空括号的隐式,例如

implicit def avroUnmarshaller: FromRequestUnmarshaller[Metrics] = ???

而不是

implicit def avroUnmarshaller(): FromRequestUnmarshaller[Metrics] = ???

(也可以改成val,不过与本期无关)