简体   繁体   中英

akka-http and JsonEntityStreamingSupport

I'm currently doing some experiment with akka and its persistency stack, wrapped with akka-http stack.

Note: For persistency, i'm using non-official plugin to persist Akka FSM to mongodb.

But my problem is using JsonEntityStreamingSupport , recommended by akka to serve Source as 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

Ok, I figured it out.

readJournal.currentPersistenceIds() gives me a Source[String, NotUsed] .

But, as it is specified in akka-http specs ,

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.

So I have to provide a Marshaller for it. 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 ""
    )
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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