本文介绍了找不到马歇尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是尝试了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:

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)
  }

这篇关于找不到马歇尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:48