4
votes

I am trying to run just the simple example from the documentation without any changes:

import spray.json.DefaultJsonProtocol
import spray.httpx.unmarshalling._
import spray.httpx.marshalling._
import spray.http._
import HttpCharsets._
import MediaTypes._

case class Person(name: String, firstName: String, age: Int)

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val PersonFormat = jsonFormat3(Person)
}

import MyJsonProtocol._
import spray.httpx.SprayJsonSupport._
import spray.util._

val bob = Person("Bob", "Parr", 32)
val body = HttpEntity(
  contentType = ContentType(`application/json`, `UTF-8`),
  string =
"""|{
  |  "name": "Bob",
  |  "firstName": "Parr",
  |  "age": 32
  |}""".stripMarginWithNewline("\n")
)

marshal(bob)
body.as[Person]

And it fails on the last line ("body.as[Person]") with the following error and stacktrace:

Exception in thread "main" java.lang.NoSuchMethodError: spray.json.JsonParser$.apply(Ljava/lang/String;)Lspray/json/JsValue;
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:36)
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:34)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
    at spray.httpx.unmarshalling.Unmarshaller$$anon$1$$anonfun$unmarshal$1.apply(Unmarshaller.scala:29)
    at spray.httpx.unmarshalling.SimpleUnmarshaller.protect(SimpleUnmarshaller.scala:40)
    at spray.httpx.unmarshalling.Unmarshaller$$anon$1.unmarshal(Unmarshaller.scala:29)
    at spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:29)
    at spray.httpx.unmarshalling.SimpleUnmarshaller.apply(SimpleUnmarshaller.scala:23)
    at spray.httpx.unmarshalling.package$PimpedHttpEntity.as(package.scala:39)
    at com.example.M1$.delayedEndpoint$com$example$M1$1(M1.scala:34)
    at com.example.M1$delayedInit$body.apply(M1.scala:3)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at com.example.M1$.main(M1.scala:3)
    at com.example.M1.main(M1.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

What can be wrong with my code? My dependencies are the following (build.sbt):

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "io.spray" %% "spray-can" % "1.3.1",
  "io.spray" %% "spray-routing" % "1.3.1",
  "io.spray" %% "spray-json" % "1.3.2",
  "com.typesafe.akka" %% "akka-actor" % "2.3.11"
)

I found a bug related to incompatibility between spray-httpx and spray-json but it seems that it has already been resolved. I am using the most recent stable versions of all libraries.

What else can be wrong?

1
It seems to be an issue with versions... any reason why you are using 1.3.1 for span-can, spray-routing and 1.3.2 for spray-json? - mericano1
Btw. why did you assume that 1.3.1 is the latest version? We got several issues like this, so maybe there's an old source where this could be fixed? (In the first place, we should have made sure not to break compatibility, but we mixed it up unfortunately) - jrudolph
I thought that 1.3.1 is the latest version because I used mvnrepository.com search. :) Check this mvnrepository.com/artifact/io.spray/spray-can/1.3.1 That was my mistake. Will know that it's not the best choice to search dependency library for java/scala. - pkozlov

1 Answers

6
votes

Looks like a version incompatibility issue between spray-http and spray-json, these specific versions work well without any problems:

libraryDependencies ++= Seq(
  "io.spray" %% "spray-can" % "1.3.3",
  "io.spray" %% "spray-routing" % "1.3.3",
  "io.spray" %% "spray-json" % "1.3.2",
  "com.typesafe.akka" %% "akka-actor" % "2.3.11"
)