找不到参数marshaller的隐式值:spray.httpx.marshalling.ToResponseMarshaller [英] could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller
问题描述
我正在使用
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 [List [org.bwi.models.Cluster]]
could not find implicit value for parameter 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 *****
}
}
}
}
}
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.
推荐答案
您需要确保Cluster
类型的隐式JsonFormat
在范围内,以便SprayJsonSupport
知道如何编组该类型.这样一来,您应该自动获得对从默认格式编组List[Cluster]
的支持.
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.
在发布的代码中,val impCluster = jsonFormat2(Cluster)
定义了JsonFormat
,但未将其标记为implicit
,因此无法隐式解析类型类.将其更改为
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)
应解决此问题.
这篇关于找不到参数marshaller的隐式值:spray.httpx.marshalling.ToResponseMarshaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!