scala 如何将 akka http 请求实体解组为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30844321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unmarshall akka http request entity as string?
提问by Ixx
I'm trying to unmarshall request payload as string, but for some reason it's failing. My code:
我正在尝试将请求有效负载解组为字符串,但由于某种原因它失败了。我的代码:
path("mypath") {
post {
decodeRequest {
entity(as[String]) {jsonStr => //could not find implicit value for...FromRequestUnmarshaller[String]
complete {
val json: JsObject = Json.parse(jsonStr).as[JsObject]
val jsObjectFuture: Future[JsObject] = MyDatabase.addListItem(json)
jsObjectFuture.map(_.as[String])
}
}
}
}
}
In this SO threadfor example it seems to be that this implicit should be available by default. But maybe this is different in akka-http?
例如,在这个SO 线程中,默认情况下似乎应该可以使用这个隐式。但也许这在 akka-http 中有所不同?
I tried importing akka.http.scaladsl.unmarshalling.PredefinedFromEntityUnmarshallerswhich has a stringUnmarshallerbut it doesn't help. Maybe because this returns type FromEntityUnmarshaller[String]not FromRequestUnmarshaller[String]. There's also a string unmarshaller in spray.httpx.unmarshalling.BasicUnmarshallersbut this also doesn't help, neither akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers
我尝试导入akka.http.scaladsl.unmarshalling.PredefinedFromEntityUnmarshallers它,stringUnmarshaller但它没有帮助。也许是因为这返回类型FromEntityUnmarshaller[String]not FromRequestUnmarshaller[String]。还有一个字符串解组器,spray.httpx.unmarshalling.BasicUnmarshallers但这也无济于事,也没有akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers
How can I unmarshall (and marshall) into a string?
我怎样才能解组(和编组)成一个字符串?
(Bonus: How to unmarshall directly in a JsObject (play json). But also only string as I'm interested in why this is not working and it may be useful for other cases).
(奖励:如何直接在 JsObject 中解组(播放 json)。但也只有字符串,因为我对为什么这不起作用很感兴趣,它可能对其他情况有用)。
Using 1.0-RC3
使用 1.0-RC3
Thanks.
谢谢。
回答by cmbaxter
Your code should be okay provided you have the right implicits in scope. If you have an implicit FlowMaterializerin scope then things should work as expected as this code that compiles shows:
你的代码应该没问题,只要你在范围内有正确的隐含。如果你有一个隐含FlowMaterializer的范围,那么事情应该按预期工作,因为编译的代码显示:
import akka.http.scaladsl.server.Route
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.stream.FlowMaterializer
implicit val system = ActorSystem("test")
implicit val mater = ActorFlowMaterializer()
val routes:Route = {
post{
decodeRequest{
entity(as[String]){ str =>
complete(OK, str)
}
}
}
}
If you wanted to take things a step further and unmarshall to a JsObjectthen you just need an implicit Unmarshallerin scope to handle that conversion, something like this:
如果您想更进一步并解组到 a ,JsObject那么您只需要一个隐式Unmarshaller的范围来处理该转换,如下所示:
implicit val system = ActorSystem("test")
implicit val mater = ActorFlowMaterializer()
import akka.http.scaladsl.unmarshalling.Unmarshaller
import akka.http.scaladsl.model.HttpEntity
implicit val um:Unmarshaller[HttpEntity, JsObject] = {
Unmarshaller.byteStringUnmarshaller.mapWithCharset { (data, charset) =>
Json.parse(data.toArray).as[JsObject]
}
}
val routes:Route = {
post{
decodeRequest{
entity(as[String]){ str =>
complete(OK, str)
}
}
} ~
(post & path("/foo/baz") & entity(as[JsObject])){ baz =>
complete(OK, baz.toString)
}
}

