喷雾收集 ToResponseMarshallable

spray Collection ToResponseMarshallable

我正在尝试 return 从我的 spray-routing 完整指令中获取一个列表。

complete {
  List("hello")
}

但是,我得到一个错误 -

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

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

知道如何使用 spray-json 编组 List/Seq 吗?还是我必须自己编写 Marshaller?

(我的scala版本是2.11.4)

使用 spray 1.3.2 和 spray-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 returns ["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"
)

API 的编组似乎自 akka 2.3.5 以来发生了变化。对于 akka-2.4.11.2SprayJsonSupport 需要 import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

1) 以下是工作 sbt,

name := "some-project"

version := "1.0"

scalaVersion := "2.11.5"

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

val akkaVersion = "2.4.5"

libraryDependencies ++= {
  Seq(
    "com.typesafe.akka" %% "akka-http-experimental" % akkaVersion,
    "com.typesafe.akka" % "akka-http-spray-json-experimental_2.11" % akkaVersion,
    "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,

    "org.mongodb" % "mongo-java-driver" % "3.4.1",
    "org.apache.kafka" %% "kafka" % "0.10.1.1",
    "org.apache.kafka" % "kafka-clients" % "0.10.1.1",

    //test
    "org.scalatest" %% "scalatest" % "2.2.5" % "test",
    "com.typesafe.akka" %% "akka-http-testkit" % "10.0.9" % "test",
    "de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "2.0.0" % "test"
  )
}

parallelExecution in Test := false

2) 并且导入是,

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

object GetMusicResources {

  val music_get_api =
    path("music") {
      get {
        complete {
          List("my data1")
        }
      }
    }
}

3) 测试

  Get("/music") ~> GetMusicResources.music_get_api ~> check {
    val response1: Future[ByteString] = response.entity.toStrict(300.millis).map {_.data }

    response1.map(_.utf8String).map { responseString =>
      responseString shouldBe """["my data1"]"""
    }

  }

就我而言,我在 :

中缺少第二个导入
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._  // <= this one

以防有人遇到同样的问题