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

Mat*_*zok 3 scala akka-stream akka-http

我想使用Alpakka处理S3上传和下载Akka Steams.但是,我在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)
Run Code Online (Sandbox Code Playgroud)

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

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

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)
}
Run Code Online (Sandbox Code Playgroud)

你有什么建议吗,我可以尝试一下吗?我在用

libraryDependencies += "com.typesafe.akka"%% "akka-http" % "10.0.5"
Run Code Online (Sandbox Code Playgroud)

Ste*_*tti 8

您需要HttpEntity从源创建一个,并为其提供内容类型.

complete(HttpEntity(ContentTypes.`application/json`, source))
Run Code Online (Sandbox Code Playgroud)