【问题标题】:akka http download marshalling in akka http testkitakka http 下载 akka http testkit 中的编组
【发布时间】:2020-03-16 14:35:12
【问题描述】:

我正在尝试在我的 akka http 测试中解组 csv 下载:

val bseq: immutable.Seq[ByteString] = Await.result(response.entity.dataBytes.runWith(Sink.seq), 20 seconds)
        val str = bseq.map(_.utf8String).mkString
        logger.debug(s"res:${str}")

但我明白了:

akka.http.scaladsl.marshalling.NoStrictlyCompatibleElementMarshallingAvailableException: None of the available marshallings (List(WithFixedContentType(application/octet-stream,<function0>))) directly match the ContentType requested by the top-level streamed entity (text/csv; charset=UTF-8). Please provide an implicit `Marshaller[akka.util.ByteString, HttpEntity]` that can render akka.util.ByteString as [text/csv; charset=UTF-8]
    at akka.http.scaladsl.marshalling.LowPriorityToResponseMarshallerImplicits$$anonfun$fromEntityStreamingSupportAndByteStringSourceMarshaller$1$$anonfun$apply$5$$anonfun$4$$anonfun$6$$anonfun$apply$7.apply(PredefinedToResponseMarshallers.scala:117)

如何在我的测试用例中解组 csv 文件?

【问题讨论】:

    标签: scala akka-http


    【解决方案1】:

    问题出在路线方面。 具体来说,csvMarshaller 不见了。

                import kantan.csv._
                import kantan.csv.ops._
    
                val src = StreamConverters
                  .asOutputStream()
                  .mapMaterializedValue { os =>
                    Future {
                      try {
                        val ps = List(Person(0, "Nicolas", 38), Person(1, "Kazuma", 1), Person(2, "John", 18))
    
                        implicit val personEncoder: RowEncoder[Person] = RowEncoder.caseEncoder(0, 2, 1)(Person.unapply)
    
                        val writer = os.asCsvWriter[Person](rfc.withHeader("Column 1", "Column 2", "Column 3"))
                        ps.foreach { p =>
                          writer.write(p)
                        }
                        writer.close()
                      } catch {
                        case e: Throwable => logger.error("failed", e)
                      }
                    }
                  }
      implicit val csvStreaming: CsvEntityStreamingSupport = EntityStreamingSupport.csv()
    // the missing csvMarshaller causes a runtime exception
                implicit val csvMarshaller: ToEntityMarshaller[ByteString] =
                  Marshaller.withFixedContentType(ContentTypes.`text/csv(UTF-8)`) { bytes =>
                    HttpEntity(ContentTypes.`text/csv(UTF-8)`, bytes)
                  }
      complete(src)
    

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多