spray Collection ToResponseMarshallable

时间:2015-02-18 02:43:34

标签: scala scala-collections spray spray-json

我正在尝试从喷涂路由中的完整指令返回一个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)

3 个答案:

答案 0 :(得分: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返回["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"
)

答案 1 :(得分:0)

akka 2.3.5以来编组的API似乎发生了变化。对于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"]"""
    }

  }

答案 2 :(得分:0)

就我而言,我缺少第二个导入:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._  // <= this one

万一有人遇到同一问题