Managing the payload with JSON-P and JSON-B
While RESTful services in general and JAX-RS applications specifically can serve entities of any media type (plain text, Extensible Markup Language (XML), Portable Document Format (PDF), binary, and so on), JSON is the standard du jour for cloud-native applications. JSON is popular because it is both human-readable and easily parsed— libraries for JSON parsing and binding exist in virtually all modern languages.
In the Entity providers section, we got a taste of serializing and deserializing a Java object (Person
) into JSON. In that section, we performed this by hand using string manipulation. While a manual approach can work, we're now going to discuss two APIs that enable simpler and more powerful control over JSON in Java.
JSON-P is a programmatic API for manipulating JSON, while JSON-B is a declarative (annotation-based) API for quickly and easily mapping an object to JSON or vice versa.
JSON-P
JSON-P has an object...