could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller
Asked Answered
C

1

3

I'm using

val akkaV = "2.2.3"
val sprayV = "1.2.0"
Seq(
  "io.spray"            %   "spray-can"     % sprayV,
  "io.spray"            %   "spray-routing" % sprayV,
  "io.spray"          %%  "spray-json"    % "1.2.5",
  "io.spray"            %   "spray-testkit" % sprayV,
  "com.typesafe.akka"   %%  "akka-actor"    % akkaV,
  "com.typesafe.akka"   %%  "akka-testkit"  % akkaV,

And getting this error:

could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[List[org.bwi.models.Cluster]]

with this code:

object JsonImplicits extends DefaultJsonProtocol {
val impCluster = jsonFormat2(Cluster)

}

trait ToolsService extends HttpService with spray.httpx.SprayJsonSupport {

val myRoute = {

    import JsonImplicits._

    path("") { get { getFromResource("tools.html") } } ~
        pathPrefix("css") { get { getFromResourceDirectory("css") } } ~
        pathPrefix("fonts") { get { getFromResourceDirectory("fonts") } } ~
        pathPrefix("js") { get { getFromResourceDirectory("js") } } ~
        path("clusters") {
            get {
                complete {
                    val result: List[Cluster] = List(Cluster("1", "1 d"), Cluster("2", "2 d"), Cluster("3", "3 d"))
                    result //*****   ERROR OCCURS HERE *****
                }
            }
        }
}

}

I've tried the suggestion on this question but it did not work, same error.

I can't seem to figure out what the implicit I need to import is. Any help would be appreciated.

Combustible answered 5/12, 2013 at 19:22 Comment(4)
If you complete the request with just a Cluster instead of a List[Cluster], does it compile? Is the implicit jsonFormat for cluster in JsonImplicits?Newly
No, it doesn't. Same error.Combustible
It probably can't find the JsonFormat for Cluster. In the above impCluster is not implicit though it should be. Is that a copy/paste error, or is that what's in your code?Newly
That was it. Make it the answer and I'll mark it for you!!Combustible
N
5

You need to make sure that the implicit JsonFormat for the Cluster type is in scope, so that SprayJsonSupport knows how to marshall that type. With that you should automatically get support for marshaling List[Cluster] from the default formats.

In the posted code val impCluster = jsonFormat2(Cluster) defines the JsonFormat, but it is not marked as implicit, so the typeclass cannot be implicitly resolved. Changing it to

implicit val impCluster = jsonFormat2(Cluster)

should fix the issue.

Newly answered 5/12, 2013 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.