A common way to use the REST protocol is to transmit a Java Bean’s content in the message body. For this option to work, we need to have a method that will allow us to marshal the Java object to and from a suitable data format. These data formats (suitable for encoding Java objects), are supported by the REST DSL:
JSON
JSON (JavaScript object notation) is a lightweight data format that can easily be mapped to and from Java objects. The JSON syntax is compact, lightly typed, and easy for humans to read and write. For all of these reasons, JSON has become popular as a message format for REST services.
For example, lets assume the following JSON code could represent a User
bean with two property fields, id
and name
:
{ "id" : 1234, "name" : "Jane Doe" }
JAXB
JAXB (Java Architecture for XML Binding) is an XML-based data format we can use for mapping to and from Java objects. Thus, to marshal the XML to a Java object, you need to annotate the Java class that you want to use.
For example, lets assume the following JAXB code could represent a User
bean with two property fields, id
and name
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <User> <Id>1234</Id> <Name>Jane Doe</Name> </User>