加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – spray Collection ToResponseMarshallable

发布时间:2020-12-16 18:39:11 所属栏目:安全 来源:网络整理
导读:我试图从喷涂路由中的完整指令返回一个List. complete { List("hello")} 但是,我收到一个错误 – Expression of type List[String] doesn't conform to expected type ToResponseMarshallable 我和Seq有同样的错误.我看到默认情况下不会在spray-httpx docume
我试图从喷涂路由中的完整指令返回一个List.

complete {
  List("hello")
}

但是,我收到一个错误 –

Expression of type List[String] doesn't conform to expected type ToResponseMarshallable

我和Seq有同样的错误.我看到默认情况下不会在spray-httpx documentation中提供List和Seq的marshallers.但是,spray-json在其DefaultJsonProtocol中提供了编组支持.我在我的代码中导入了spray.httpx.SprayJsonSupport._和spray.json.DefaultJsonProtocol._,但这也没有帮助.

知道如何使用spray-json编组List / Seq?或者我必须写自己的Marshaller?

(我的scala版本是2.11.4)

解决方法

使用喷雾1.3.2和喷雾json 1.3.2应该是可能的.

确保你导入(虽然你说你做,但仔细检查).

import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

考虑以下示例:

import akka.actor.{ActorSystem,Props,Actor}
import akka.io.IO
import spray.can.Http
import spray.routing.HttpService
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

object Boot extends App {
  implicit val system = ActorSystem("so")

  val testActor = system.actorOf(Props[TestActor])

  implicit val timeout = Timeout(5.seconds)
  IO(Http) ? Http.Bind(testActor,interface = "0.0.0.0",port = 5000)

}

class TestActor extends Actor with HttpService {

  def receive  = runRoute(route)

  def actorRefFactory = context

  val route = get{
    path("ping") {
      complete(List("OK"))
    }
  }

}

请求/ ping返回[“OK”],看起来没问题.

仅供参考build.sbt以下.

build.sbt

val akkaVersion = "2.3.5"

val sprayVersion = "1.3.2"

resolvers += "spray" at "http://repo.spray.io/"

scalaVersion := "2.11.5"

scalacOptions := Seq("-feature","-unchecked","-deprecation","-encoding","utf8")

libraryDependencies ++= Seq(
  "com.typesafe.akka"   %% "akka-actor"       % akkaVersion,"io.spray"            %% "spray-can"        % sprayVersion,"io.spray"            %% "spray-routing"    % sprayVersion,"io.spray"            %% "spray-client"     % sprayVersion,"io.spray"            %% "spray-json"       % "1.3.1"
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读