[英]akka-http and JsonEntityStreamingSupport
我目前正在對akka及其持久性堆棧(用akka-http堆棧包裝)進行一些實驗。
注意:為了保持持久性,我正在使用非官方插件將Akka FSM持久化到mongodb。
但是我的問題是使用JsonEntityStreamingSupport
推薦的JsonEntityStreamingSupport來將Source
用作json 。
就我而言,我有這段代碼
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)
但是不幸的是,我遇到了這個錯誤:
$ curl localhost:8081/workflows
curl: (56) Recv failure: Connection reset by peer
我沒有看到任何錯誤或日志,這些錯誤或日志可能會導致有關服務器為何關閉連接的信息。
有沒有人做過這種實驗?
我正在用akka 2.4.16和akka-http 10.0.5進行測試
好的,我知道了。
readJournal.currentPersistenceIds()
給我一個Source[String, NotUsed]
。
但是,正如akka-http規范中指定的那樣 ,
這是錯誤的,因為我們嘗試呈現JSON,但是String不是有效的頂級元素,如果我們確實要呈現字符串列表,則需要提供顯式的Marshaller [String,ByteString]。
因此,我必須為此提供一名Marshaller
。 例如,通過這些相同的測試給出:
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.