4

I am trying to use my domain objects as a request/response body parameters. I am using spray-routing and as[T] to unmarshall object. But constantly I'm getting could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller. Eventhough I've added implicit unmarshaller manually to companion object I'm getting same error. I have no idea what is wrong. This JSON serializer/deserializer for Event I've written because I need to serialize joda DateTime object.

package services

import spray.routing.HttpService
import org.joda.time.DateTime
import org.joda.time.format.{DateTimeFormatter, ISODateTimeFormat}
import spray.httpx.unmarshalling.FromRequestUnmarshaller
import spray.json._
import services.EventsService.Event
import spray.httpx.SprayJsonSupport

trait EventsService extends HttpService {

  val eventsRoute =
    path("/events") {
      get {
        import EventsService._
        entity(as[Event]) { event =>
          complete(s"${event.toString}")
        }
      }
    }
}

object EventsService extends DefaultJsonProtocol with SprayJsonSupport{
  trait DateFormatter {
    val formatter: DateTimeFormatter
  }

  trait DateParser {
    val parser: DateTimeFormatter
  }

  implicit object EventFormatter extends RootJsonFormat[Event] with DateFormatter with DateParser {
    def read(json: JsValue): Event = json match {
      case obj: JsObject =>
        val name = obj.fields.get("name").map(_.asInstanceOf[JsString].value).
          getOrElse(deserializationError("field name not present"))
        val city = obj.fields.get("city").map(_.asInstanceOf[JsString].value).
          getOrElse(deserializationError("field city not present"))
        val starts = obj.fields.get("starts").map(x => parser.parseDateTime(x.asInstanceOf[JsString].value)).
          getOrElse(deserializationError("field starts not present"))
        val ends = obj.fields.get("ends").map(x => parser.parseDateTime(x.asInstanceOf[JsString].value)).
          getOrElse(deserializationError("ends field not present"))
        Event(name, city, starts, ends)
      case _ => deserializationError("wrong object to deserialize")
    }

    def write(e: Event): JsValue =
      JsObject(Map(
        "name" -> JsString(e.name),
        "city" -> JsString(e.city),
        "starts" -> JsString(formatter.print(e.starts)),
        "ends" -> JsString(formatter.print(e.ends))
      ))

    val formatter = ISODateTimeFormat.dateTimeNoMillis()
    val parser = ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed()
  }

  case class Event(name: String, city: String, starts: DateTime, ends: DateTime)
}
bkowalikpl
  • 817
  • 5
  • 11
  • I'm not sure you're supposed to be explicitly importing `FromRequestUnmarshaller`. Have a look at [`MarshallingDirectives`](https://github.com/spray/spray/blob/3fecb9fc3859f627e4c98b718f6347b27cd03b8f/spray-routing/src/main/scala/spray/routing/directives/MarshallingDirectives.scala) available as in import from `spray.routing` package and then use those available highlevel handler methods in it. – S.R.I May 19 '14 at 07:47

1 Answers1

2

Ok, I've figured that out. The order of structures is wrong. First should be companion object and second trait with route. I don't know why, yet but it works now.

bkowalikpl
  • 817
  • 5
  • 11
  • 2
    Good catch. Yes, that's a "feature" of Scala implicit resolution rules: to avoid ambiguities and cycles in the type inference, Scala only regards implicit definitions in the same file that are *above* when searching for an implicit value. – jrudolph May 19 '14 at 10:56