Spray ToResponseMarshallable Collection

I am trying to return a list from my complete directive in spray routing.

complete { List("hello") } 

However, I get an error -

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

I get the same error with Seq. I see that marshlers for List and Seq are not provided by default in spray-httpx documentation . However, spray-json provides marshalling support in its DefaultJsonProtocol. I imported spray.httpx.SprayJsonSupport._ and spray.json.DefaultJsonProtocol._ into my code, but that didn't help either.

Any idea how I can marshal List / Seq using spray json? Or will I have to write my own Marshaller?

(My version of scala is 2.11.4)

+5
source share
2 answers

Using spray 1.3.2 and spray-json 1.3.2, this should be possible.

Make sure you import (although you say what you do, but double check).

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

Consider the following example:

 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")) } } } 

The /ping request returns ["OK"] , which looks fine.

For reference build.sbt below.

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" ) 
+4
source

The sorting API seems to have changed from akka 2.3.5 . For akka-2.4.11.2 , SprayJsonSupport requires import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

1) Below is the work of 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) And imported

 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) Tests

  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
source

Source: https://habr.com/ru/post/1213582/


All Articles