Populating Java objects from JSON with JSON-B
A common programming task is to populate Java objects from JSON strings. It is such a common tasks that several libraries have been created to transparently populate Java objects from JSON, freeing application developers from having to manually code this functionality. There are some non-standard Java libraries that accomplish this task, such as Jackson (https://github.com/FasterXML/jackson), JSON-simple (https://github.com/fangyidong/json-simple), and Gson (https://github.com/google/gson). Java EE 8 introduces a new API providing this functionality, namely the Java API for JSON Binding (JSON-B). In this section, we will cover how to transparently populate a Java object from a JSON string.
The following example shows a RESTful web service written using the Java API for RESTful Web Services (JAX-RS). The service responds to HTTP POST requests in its addCustomer()
method. This method takes a String
as a parameter and this string is expected to contain...