+doc #19796 explain how to obtain a JsValue using entity(as)

This commit is contained in:
Konrad Malawski 2016-02-16 15:20:46 +01:00
parent 61a86556c6
commit 1143b27b89
2 changed files with 25 additions and 1 deletions

View file

@ -8,7 +8,7 @@ import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import docs.http.scaladsl.server.RoutingSpec
import akka.http.scaladsl.model.MediaTypes.`application/json`
import akka.http.scaladsl.model._
import spray.json.DefaultJsonProtocol
import spray.json.{ JsValue, DefaultJsonProtocol }
//# person-case-class
case class Person(name: String, favoriteNumber: Int)
@ -37,6 +37,22 @@ class MarshallingDirectivesExamplesSpec extends RoutingSpec {
}
}
"example-entity-with-raw-json" in {
import PersonJsonSupport._
val route = post {
entity(as[JsValue]) { json =>
complete(s"Person: ${json.asJsObject.fields("name")} - favorite number: ${json.asJsObject.fields("favoriteNumber")}")
}
}
// tests:
Post("/", HttpEntity(`application/json`, """{ "name": "Jane", "favoriteNumber" : 42 }""")) ~>
route ~> check {
responseAs[String] shouldEqual """Person: "Jane" - favorite number: 42"""
}
}
"example-completeWith-with-json" in {
import PersonJsonSupport._