大佬教程收集整理的这篇文章主要介绍了json – 如何将akka http请求实体unmarshall作为字符串?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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]) } } } } }
在这个SO thread中,例如,似乎默认情况下这个隐含的可用.但是也许这在akka-http中是不同的?
我尝试导入akka.http.scaladsl.unmarshalling.PredefinedFromEntityUnmarshallers,它具有一个stringUnmarshaller,但没有帮助.也许因为这样返回类型FromEntityUnmarshaller [String]不是FromRequestUnmarshaller [String].在spray.httpx.unmarshalling.BasicUnmarshallers中还有一个字符串解组器,但这也没有帮助,既不是akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers
我如何解组(和编组)成一个字符串?
(Bonus:如何在JsObject(播放json)中直接解组,但也只有字符串,因为我有兴趣为什么这不工作,它可能对其他情况有用).
使用1.0-RC3
谢谢.
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) } } } }
如果你想要进一步的进一步解决一个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) } }
以上是大佬教程为你收集整理的json – 如何将akka http请求实体unmarshall作为字符串?全部内容,希望文章能够帮你解决json – 如何将akka http请求实体unmarshall作为字符串?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。