我想使用 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/