Jim*_*ows 3 scala spray spray-json
我正在使用
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,
Run Code Online (Sandbox Code Playgroud)
并收到此错误:
找不到参数marshaller的隐含值:spray.httpx.marshalling.ToResponseMarshaller [List [org.bwi.models.Cluster]]
使用此代码:
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 *****
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
我似乎无法弄清楚我需要导入的隐含内容.任何帮助,将不胜感激.
你需要确保隐含JsonFormat的Cluster类型范围,让SprayJsonSupport知道如何马歇尔该类型.有了它,您应该自动获得List[Cluster]默认格式的编组支持.
在发布的代码中val impCluster = jsonFormat2(Cluster)定义了JsonFormat,但它没有标记为implicit,因此类型类无法隐式解析.把它改成
implicit val impCluster = jsonFormat2(Cluster)
Run Code Online (Sandbox Code Playgroud)
应该解决这个问题.