喷射集合 ToResponseMarshallable。

6
我想在spray-routing中从我的complete指令返回一个列表。
complete {
  List("hello")
}

然而,我遇到了一个错误 -

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

我在使用Seq时遇到了相同的错误。我发现spray-httpx默认没有提供List和Seq的编组器documentation。然而,spray-json在其DefaultJsonProtocol中提供了编组支持。我已经在我的代码中导入了spray.httpx.SprayJsonSupport._和spray.json.DefaultJsonProtocol._,但这也没有帮助。
有什么办法可以使用spray-json编组List/Seq吗?还是我必须编写自己的编组器?
(我的scala版本是2.11.4)
3个回答

5

使用 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
是的,这个可以工作。但是如果我用大括号代替括号,它就会失败。我正在做complete{List("OK")}。我的实际情况需要一个代码块。你知道为什么用大括号可能会失败吗? - adefor
当我在我的示例中将 ( 替换为 {,并将 ) 替换为 } 时,它可以工作。所以你确定这是你所做的唯一更改吗? - lpiepiora
3
好的。我认为只是IntelliJ出了些问题。当我构建和运行我的项目时,它可以正常工作。谢谢! - adefor

1
API for marshalling在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"]"""
    }

  }

0
在我的情况下,我错过了“:

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

如果有人遇到相同的问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接