2017-03-28 55 views
2

我目前正在做一些實驗akka及其持久性堆棧,用akka-http堆棧包裝。akka-http和JsonEntityStreamingSupport

注意:爲了持久性,我使用非官方插件將Akka FSM保留爲mongodb。

但我的問題是使用JsonEntityStreamingSupportrecommended by akka to serve Source as 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 

我沒有看到任何錯誤或日誌,這可能會導致信息關於服務器關閉連接的原因。

有沒有人已經做過這種實驗?

我與阿卡2.4.16和測試它阿卡-HTTP 10.0.5

回答

2

好吧,我想通了。

readJournal.currentPersistenceIds()給我一個Source[String, NotUsed]

但是,因爲它是在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.

所以,我必須爲它提供一個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 "" 
    ) 
    } 
}