首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >akka http testkit中的akka http下载编组

akka http testkit中的akka http下载编组
EN

Stack Overflow用户
提问于 2020-03-16 22:35:13
回答 1查看 74关注 0票数 0

我正试图在我的akka http测试中解组一个csv下载:

代码语言:javascript
复制
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}")

但我得到了:

代码语言:javascript
复制
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文件?

EN

回答 1

Stack Overflow用户

发布于 2020-03-17 02:02:01

问题出在路由方面。具体地说,csvMarshaller丢失了。

代码语言:javascript
复制
            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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60707890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档