找不到参数marshaller的隐含值:spray.httpx.marshalling.ToResponseMarshaller

 wang静的天空 发布于 2023-02-12 16:22

我正在使用

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,

并收到此错误:

找不到参数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 *****
                }
            }
        }
}

}

我已经尝试过关于这个问题的建议,但它没有用,同样的错误.

我似乎无法弄清楚我需要导入的隐含内容.任何帮助,将不胜感激.

1 个回答
  • 你需要确保隐含JsonFormatCluster类型范围,让SprayJsonSupport知道如何马歇尔该类型.有了它,您应该自动获得List[Cluster]默认格式的编组支持.

    在发布的代码中val impCluster = jsonFormat2(Cluster)定义了JsonFormat,但它没有标记为implicit,因此类型类无法隐式解析.把它改成

    implicit val impCluster = jsonFormat2(Cluster)
    

    应该解决这个问题.

    2023-02-12 16:25 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有