A suite of scala libraries for building and consuming RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, testable
code
def rejection(implicit logging: LoggingContext) = case MalformedQueryParamRejection(param, message, error) :: _ => ctx =>
respondToBadRequest(ctx, s"Malformed params: $param")
case MissingQueryParamRejection(param) :: _ => ctx =>
respondToBadRequest(ctx, s"Missing params: $param")
case MissingFormFieldRejection(field) :: _ => ctx =>
respondToBadRequest(ctx, s"Missing field $field")
case MalformedFormFieldRejection(field, error, cause) :: _ => ctx =>
respondToBadRequest(ctx, s"Malformed field $field. Error: $error")
case MalformedRequestContentRejection(message, cause) :: _ => ctx =>
respondToBadRequest(ctx, message)
case RequestEntityExpectedRejection :: _ => ctx => {
val message =
s"""Request entity expected but not supplied.
| Target: ${ctx.request.unmatchedPath}
| Entity: ${ctx.request.entity.asString}
"""
respondToBadRequest(ctx, message)
}
case SchemeRejection(message) :: _ => ctx =>
respondToBadRequest(ctx, s"Schema rejection. Error: $message")
case MethodRejection(message) :: _ => ctx =>
respondToRejection(ctx, s"Method not allowed on this resource. Error $message", MethodNotAllowed)
case UnsupportedRequestContentTypeRejection(error) :: _ => ctx =>
respondToBadRequest(ctx, error)
case ValidationRejection(message, cause) :: _ => ctx =>
respondToBadRequest(ctx, s"Validation rejection: $message")
}
import com.konga.order.models.OrderJsonSupport._
import DefaultJsonProtocol._
RouteTest
in the testkit appears to eagerly create and start the actor system. While this normally wouldn't be an issue, we use ScalaTest's tagging to include/exclude tests. The eager loading slows things down considerably as the actor system will be initialized even though no tests are actually run.
@shankarshastri you can use spray-json.
import spray.json._
val jsonString: String = ???
val json: JsObject = jsonString.parseJson.asJsObject
or you can declare a class describing this json and declare format for it and then convert to it directly:
class JsonRepr(???)
implicit object ReprFormat extends RootJsonFormat[JsonRepr]{
???
}
import spray.json._
val jsonString: String = ???
jsonString.parseJson.convertTo[ReprFormat]
jsonString.parseJson.asJsObject.fields
will return Map[String, JsValue]