scala - 在 Akka-Http 中完成 Source[ByteString, _]

标签 scala akka-http akka-stream

我想使用 Alpakka 通过 Akka Steams 处理 S3 上传和下载。但是,我在 Akka Http 路由中使用 S3Client 生成的 Source 时遇到了困难。我得到的错误信息是:

[error]  found   : akka.stream.scaladsl.Source[akka.util.ByteString,_$1] where type _$1
[error]  required: akka.http.scaladsl.marshalling.ToResponseMarshallable
[error]     complete(source)

我认为这是一些烦人的琐碎事情,比如缺少隐式导入,但我无法确定我缺少什么。

我创建了一些最小的例子来说明这个问题:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.util.ByteString

import scala.concurrent.ExecutionContext

class Test {

  implicit val actorSystem: ActorSystem = ActorSystem()
  implicit val materializer: ActorMaterializer = ActorMaterializer()
  implicit val executionContext: ExecutionContext = actorSystem.dispatcher

  val route = (path("test") & get) {
    def source: Source[ByteString, _] = ??? // just assume that I am able to get that value
    complete(source) // here error happens
  }

  Http().bindAndHandle(route, "localhost", 8000)
}

您有什么建议,我可以尝试什么?我正在使用

libraryDependencies += "com.typesafe.akka"%% "akka-http" % "10.0.5"

最佳答案

你需要从源创建一个HttpEntity,并给它一个内容类型。

complete(HttpEntity(ContentTypes.`application/json`, source))

关于scala - 在 Akka-Http 中完成 Source[ByteString, _],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913094/

相关文章:

scala 为外部作用域创建 val

scala - 将 Akka 流连接到 JMS

scala - 将单个参数和 Seq 传递给 var-arg 函数

json - 使用 Akka Http 和 Circe 在 Scala 中解码 JSON

scala - 来自字符串的动态 PathMatcher

scala - akka流http速率限制

scala - Akka 流 : dealing with futures within graph stage

scala - 在 Scala 中拦截套接字关闭上的 Akka HTTP WebSocket 事件

scala - Either模式匹配的替代方案

html - 如何将视频从 Play 2 Framework 流式传输到 HTML5 Play 器