I'm about to start on a small project which goal is to end up with a web xml/json api. I'll be writing it in Java, and I'll be using the restlet library.
How do I approach the xml/json duality? I know I can use JAXB to "convert" pojos to xml (and back), but how do I automate this for json? Is there any functionality in the restlet library I can leverage?
Restlet allows you to directly work with POJOs within your server resources at the level of REST annotated methods, as described below:
You don't need to specify media type (i.e. content type) within the content of the annotations.
To handle such code, Restlet provides a generic conversion feature that does the work under the hood. Restlet extensions provide different implementations for this feature. If you want to have both XML and JSON for your RESTful application, I think that you should the Jackson extension for what you want to do. Jackson (http://wiki.fasterxml.com/JacksonHome) is a tool that allows to convert POJO to various formats like XML, JSON and YAML.
To enable this, simply put the JAR file of the Jackson extension (org.restlet.ext.jackson) in your classpath and use the programming approach described above. Here are the details on how it works:
You will be able to send both JSON and XML content (set the header
Content-Type
in the request) and Restlet will automatically convert this content to the bean expected in the annotated method.To switch / select the expected content with response, you can leverage to conneg (content negociation of REST) based on the header
Accept
. If you specifyapplication/json
, a JSON content wil be received andapplicaiton/xml
, a XML one.You can notice that Restlet also support a query parameter
media
(not standard) to select the content to receive. If you specifyjson
, a JSON content will be received back andxml
an XML one.To finish, you can notice that this mechanism is also supported on server-side with Restlet. This means that you can work directly with beans. This feature can be used with Restlet annotated interfaces, as described below:
You can use this interface as described below:
Don' forget to put in the client classpath application the extension Jackson.
Hope it helps, Thierry