Following the tutorial from http://www.smartjava.org/content/first-steps-rest-spray-and-scala, there are some unexpected error messages. What is going on? Have I defined implicit JsonWriter by the implicit val personFormat = jsonFormat3(Person)
call?
scala> import spray.json.DefaultJsonProtocol
import spray.json.DefaultJsonProtocol
scala> object MyJsonProtocol extends DefaultJsonProtocol {
implicit val personFormat = jsonFormat3(Person)
}
| | defined object MyJsonProtocol
scala> case class Person(name: String, fistName: String, age: Long)
defined class Person
scala> import spray.json._
import spray.json._
scala> import MyJsonProtocol._
import MyJsonProtocol._
scala> Person(name="a", fistName="b", age = 10).toJson
<console>:45: error: Cannot find JsonWriter or JsonFormat type class for Person
Person(name="a", fistName="b", age = 10).toJson
^
From your session it seems as if you define the protocol before you declare your Person
class, which would mean that you already have another Person class in scope. After defining the protocol you then re-define the Person
class, thus the format can't be found. So to sum up, make sure that you first declare your Person
class and then define your Format.
EDIT: Updated answer
来源:https://stackoverflow.com/questions/28040828/cannot-find-jsonwriter-or-jsonformat-type-class-for-a-case-class