只是尝试了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) }更多推荐
找不到马歇尔
发布评论