简体   繁体   English

akka-http和JsonEntityStreamingSupport

[英]akka-http and JsonEntityStreamingSupport

I'm currently doing some experiment with akka and its persistency stack, wrapped with akka-http stack. 我目前正在对akka及其持久性堆栈(用akka-http堆栈包装)进行一些实验。

Note: For persistency, i'm using non-official plugin to persist Akka FSM to mongodb. 注意:为了保持持久性,我正在使用非官方插件将Akka FSM持久化到mongodb。

But my problem is using JsonEntityStreamingSupport , recommended by akka to serve Source as json . 但是我的问题是使用JsonEntityStreamingSupport 推荐的JsonEntityStreamingSupport来将Source用作json

In my case, i have this piece of code 就我而言,我有这段代码

implicit val jsonEntityStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json()

val readJournal = PersistenceQuery(system).readJournalFor[ScalaDslMongoReadJournal](MongoReadJournal.Identifier)

val route =
  path("workflows") {
    get {
      complete(readJournal.currentPersistenceIds())
    }
}

Http().bindAndHandle(route, "localhost", 8081)

But unfortunately, I came with this error: 但是不幸的是,我遇到了这个错误:

$ curl localhost:8081/workflows
curl: (56) Recv failure: Connection reset by peer

I do not see any errors or logs which could lead to information about why server is closing connection. 我没有看到任何错误或日志,这些错误或日志可能会导致有关服务器为何关闭连接的信息。

Does anyone already done this kind of experiment ? 有没有人做过这种实验?

I'm testing it with akka 2.4.16 and akka-http 10.0.5 我正在用akka 2.4.16和akka-http 10.0.5进行测试

Ok, I figured it out. 好的,我知道了。

readJournal.currentPersistenceIds() gives me a Source[String, NotUsed] . readJournal.currentPersistenceIds()给我一个Source[String, NotUsed]

But, as it is specified in akka-http specs , 但是,正如akka-http规范中指定的那样

This is wrong since we try to render JSON, but String is not a valid top level element we need to provide an explicit Marshaller[String, ByteString] if we really want to render a list of strings. 这是错误的,因为我们尝试呈现JSON,但是String不是有效的顶级元素,如果我们确实要呈现字符串列表,则需要提供显式的Marshaller [String,ByteString]。

So I have to provide a Marshaller for it. 因此,我必须为此提供一名Marshaller For example, as giving by those same tests : 例如,通过这些相同的测试给出:

implicit val stringFormat = Marshaller[String, ByteString] { ec ⇒ s ⇒
  Future.successful {
    List(Marshalling.WithFixedContentType(ContentTypes.`application/json`, () ⇒
      ByteString("\"" + s + "\"")) // "raw string" to be rendered as json element in our stream must be enclosed by ""
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM