spray-json error: could not find implicit value for parameter um
Asked Answered
A

2

13

I have this case class

case class Person(val name: String)

object JsonImplicits extends DefaultJsonProtocol {
  implicit val impPerson = jsonFormat1(Person)
}

I'm trying spray-json in order to parse post request:

  post {
    entity(as[Person]) { person =>
      complete(person)
    }
  }

However I get when I try to compile this:

src/main/scala/com/example/ServiceActor.scala:61: error: could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller[com.example.Person]

I don't understand what's happening, how can I fix this to be working?

thanks

Acclaim answered 5/1, 2014 at 9:57 Comment(2)
Did you import JsonImplicits._ in your routes?Asquith
and don't forget import spray.httpx.SprayJsonSupport._Lyso
A
14

Spray's 'entity[E]' directive requires implicit marshaller in its scope for the type E. JsonImplicits object creates json marshaller and unmarshaller for the type E.

You need to make sure that implicit val impPerson is in the scope, in other words put import JsonImplicits._ above the route definition.

Asquith answered 5/1, 2014 at 20:52 Comment(1)
I pulled my hair out with exactly the same problem. Despite I read good docs and even the same seed project codebase I didn't realised about the route had a different scope. *Scope Rule*: An inserted implicit conversion must be in scope as a single identifier, or be associated with the source or target type of the conversion. The Scala compiler will only consider implicit conversions that are in scope. To make an implicit conversion available, therefore, you must in some way bring it into scopeComedian
D
-1
package abc.json

import spray.json.DefaultJsonProtocol


object OrderJsonProtocol extends DefaultJsonProtocol {

  implicit val orderFormat = jsonFormat1(Order)
}


case class Order(orderNumber: String)

import akka.actor.Actor
import abc.json._
import spray.routing.HttpService

class OrderRestServiceActor extends Actor with HttpService {

  def actorRefFactory = context

  def receive = runRoute(route)



  val route = {
    import OrderJsonProtocol._
    import spray.httpx.SprayJsonSupport.sprayJsonUnmarshaller


    path("order") {
      post {
        println("inside the path")
        entity(as[Order]) { order =>
         complete(s"OrderNumber: ${order.orderNumber}")
        }

      }
    }

  }

}
Dempstor answered 13/8, 2015 at 20:19 Comment(1)
The above works. A lot of people say import just above and it took a long time to figure it out. Here is the codeDempstor

© 2022 - 2024 — McMap. All rights reserved.