liveBook · Manning

Akka-http-spray-json

As explained in Akka in Action, Second Edition

SprayJsonSupport is an Akka HTTP trait that supplies JSON marshalling and unmarshalling support based on Spray JSON.

akka-http-spray-json connects Akka HTTP routes to Spray JSON. It marshals Scala values into JSON responses and unmarshals JSON requests into domain objects through implicitly available formatters. section 11.1.5

A practical setup follows three steps:

  • Use built-in formats for basic values such as String, Int, Option, and List. section 11.1.5
  • Define jsonFormat2, jsonFormat1, or another formatter for case classes and nested values. section 11.1.5

Expose sharded cargo through HTTP

A route extracts entityId, kind, and size, creates Container.Cargo, and sends AddCargo to the sharding region. The response defines formats for Cargo and Cargos, then completes with the actor result. A POST adds cargo, while GET returns JSON such as {"cargos":[{"kind":"bigbag","size":4},{"kind":"sack","size":12}]}. For request bodies, entity(as[Container.Cargo]) uses the same formatter for unmarshalling. The example chooses tell for fast cargo submission and ask for querying the container state. section 11.1.6

How Akka in Action, Second Edition covers it

How deep it goes
Explains implicit marshaller lookup and composable case-class formatters, then applies them in routes without detailing serialization internals.
Examples
Uses Cargo and Cargos case classes, POST and GET cargo routes, nested list formatting, and curl requests with observable JSON output.
What it assumes
Requires familiarity with Scala case classes, Akka HTTP routes, and actor messaging while teaching formatter imports and implicit availability.
What you can do after
Apply it via SprayJsonSupport and JSON protocols to build Akka HTTP routes that encode responses and decode request entities.
How it connects
Connects JSON serialization, implicit resolution, case classes, Akka HTTP, actors, and cluster sharding.

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.