我在用
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,并得到这个错误:
找不到参数封送器的隐式值: spray.httpx.marshalling.ToResponseMarshaller[Listorg.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 *****
}
}
}
}}
我尝试过这个建议on this question,但它没有工作,同样的错误。
我似乎不知道我需要导入什么隐式。任何帮助都将不胜感激。
发布于 2013-12-05 22:42:27
您需要确保Cluster类型的隐式Cluster在作用域中,以便SprayJsonSupport知道如何对该类型进行强制转换。这样,您就可以自动从默认格式获得封送List[Cluster]的支持。
在发布的代码中,val impCluster = jsonFormat2(Cluster)定义了JsonFormat,但它没有被标记为implicit,因此不能隐式解析类型类型。改到
implicit val impCluster = jsonFormat2(Cluster)应该解决这个问题。
https://stackoverflow.com/questions/20408734
复制相似问题