Marshaller not found

Just try json spray, and it looks like I'm having trouble finding my JsonProtocols that I have installed. 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" 

And the following code:

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

I get an error in the line where I return Content in the complete {} block, the error is the following: code below:

Description Resource path location type could not find an implicit value for the proof parameter of type spray.httpx.marshalling.Marshaller [Content] MyService.scala line 32 Scala

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

Can someone see something wrong? This is literally the code of the spray pattern with spray substances sprinkled in

+1
scala
source share
1 answer

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

 import spray.httpx.SprayJsonSupport._ 

And with this marshaller, you can remove the respondWithMediaType(application/json) directive respondWithMediaType(application/json) , because Json is marshaled only for type application/json :

 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) } 
+7
source share

All Articles