JSON is supported in CXF through Jettison. Jettison is a StAX implementation that reads and writes JSON. Jettison intercepts calls to read/write XML and instead read/writes JSON.
To understand how to create a JSON service, you first need to understand how your XML structures will be converted into the JSON format. JSON does not have many concepts found in XML such as namespaces, attributes or entity references. Because of this we must adopt conventions on how to convert between the two.
Jettison supports two conventions currently. These are explained in more detail in the Jettison user's guide, but a quick summary of each follows.
1. The "mapped" convention. In this convention namespaces are mapped to json prefixes. For instance, if you have a namespace "http://acme.com" you could map it to the "acme" prefix. This means that the element <customer xmlns="http://acme.com"> would be mapped to the "acme.customer" JSON tag.
2. The BadgerFish convention. This convention provides a mapping of the full XML infoset to JSON. Attributes are represented with an @ sign - i.e. "@attributeName" : "value". Textual data is represented with the "$" as the tag. Example:
This example shows how to set up Jettison using a ServerFactoryBean, such as the JaxWsServerFactoryBean. First you must create a properties HashMap and set the StAX XMLInputFactory and XMLOutputFactory:
You must also tell CXF which Content-Type you wish to serve:
Last, you'll want to actually create your service: