找不到马歇尔

编程入门 行业动态 更新时间:2024-10-20 16:42:22
本文介绍了找不到马歇尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

只是尝试了spray-json,发现我已设置的JsonProtocols似乎有问题.我具有以下依赖关系:

Just trying out the spray-json, and it seems to be having problem finding my JsonProtocols I have setup. I have the following dependencies:

"io.spray" % "spray-servlet" % "1.2-M8", "io.spray" % "spray-routing" % "1.2-M8", "io.spray" % "spray-testkit" % "1.2-M8", "io.spray" % "spray-json_2.10" % "1.2.5"

以及以下代码:

Content.scala

import spray.json.DefaultJsonProtocol case class Content(id:String, name: String, contentType: String, duration: Int) object MyJsonProtocol extends DefaultJsonProtocol { implicit val contentFormat = jsonFormat4(Content) }

在complete {}块中返回Content的行上出现错误,错误如下,代码在其下方:

I get an error on the line where I'm returning Content in the complete {} block, the error is as follows and the code is below it:

描述资源路径位置类型 找不到类型为spray.httpx.marshalling.Marshaller [Content] MyService.scala第32行Scala问题的证据参数的隐含值

Description Resource Path Location Type could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[Content] MyService.scala line 32 Scala Problem

import akka.actor.Actor import spray.routing._ import spray.http._ import MediaTypes._ import spray.json.DefaultJsonProtocol import Content import MyJsonProtocol._ class MyServiceActor extends Actor with MyService{ def actorRefFactory = context def receive = runRoute(myRoute) } trait MyService extends HttpService { val myRoute = path("") { get { respondWithMediaType(`application/json`) { // XML is marshalled to `text/xml` by default, so we simply override here complete { new Content("1234", "Some Content", "YT", 60) } } } } }

有人可以看到任何错误吗?从字面上看,这是将Spray-json内容撒在其中的spray-template代码

Can anyone see anything wrong? This is literally the spray-template code with the spray-json stuff sprinkled in

推荐答案

Json marshaller具有SprayJsonSupport特性,因此只需将其导入范围:

Json marshaller is in SprayJsonSupport trait, so just import it into the scope:

import spray.httpx.SprayJsonSupport._

使用此编组器,您可以删除respondWithMediaType(application/json)指令,因为它仅将Json编组为application/json媒体类型:

And with this marshaller you can remove respondWithMediaType(application/json) directive, cause it Json is marshaled only to application/json media type:

implicit def sprayJsonMarshaller[T](implicit writer: RootJsonWriter[T], printer: JsonPrinter = PrettyPrinter) = Marshaller.delegate[T, String](ContentTypes.`application/json`) { value ⇒ val json = writer.write(value) printer(json) }

更多推荐

找不到马歇尔

本文发布于:2023-11-25 23:57:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:马歇尔   找不到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!