我正试图在我的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文件?
发布于 2020-03-17 02:02:01
问题出在路由方面。具体地说,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)https://stackoverflow.com/questions/60707890
复制相似问题