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

Completing Source[ByteString, _] in Akka-Http

我想使用 Alpakka 处理 S3 上传和下载 Akka Steams。但是,我坚持在 Akka Http 路由中使用 S3Client 生成的源代码。我收到的错误消息是:

[error]  found   : akka.stream.scaladsl.Source[akka.util.ByteString,_] where type _
[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))