【问题标题】:Completing Source[ByteString, _] in Akka-Http在 Akka-Http 中完成 Source[ByteString, _]
【发布时间】:2017-07-04 19:36:36
【问题描述】:

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

【问题讨论】:

    标签: scala akka-http akka-stream


    【解决方案1】:

    您需要从源中创建一个HttpEntity,并为其指定一个内容类型。

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

    【讨论】:

    • 更具体地说,HttpEntity.Default。 “它的长度已知,并将其数据显示为 Source[ByteString]”。
    • 谢谢!我对doc.akka.io/docs/akka-http/10.0.9/scala/http/routing-dsl/… 的第二段感到困惑。我(错误)理解我缺少一些隐式转换。
    • 我明白了。仅当您想直接流式传输自定义类并在后台将它们转换为 JSON 时,才需要进一步的隐式。
    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多