ConverterService

Introduction

The converter service of an application handles the conversion between representations (received by a resource for example) and beans or POJOs. It can be used either programmatically or transparently thanks to annotated Restlet resources.

Description

Server-side usage

Let’s describe how this service is typically used with a ServerResource that supports GET and PUT methods as below:

@Get("json")
// Returns a json representation of a contact.
public Contact toJson(){
   [...]
}

@Put()
// Handles a Web form in order to set the full state of the resource.
public void store(Form form){
   [...]
}

In this case, the aim of the converter service is to:

  • return a JSON representation of a contact
  • convert a Web form into a Form object and pass it to the store method.

Client-side usage

On the client-side you can leverage the service either by creating a proxy of an annotated resource interface, via ClientResource.wrap(…) or ClientResource.create(…) methods. Otherwise, it is possible to invoke any web API and specify the excepted return type and let the service convert between beans and representations. Here is the list of related methods on ClientResource:

  • delete(Class<T> resultClass) : T
  • get(Class<T> resultClass) : T
  • options(Class<T> resultClass) : T
  • post(Object entity, Class<T> resultClass) : T
  • put(Object entity, Class<T> resultClass) : T

For example if you want to retrieve an XML representation as a DOM document, you can just do:

ClientResource cr = new ClientResource("http://myapi.com/path/resource");
Document doc = cr.get(Document.class);

And that’s all you need to do, as long as you have the org.restlet.ext.xml.jar in your classpath!

Internals of the service

A converter service does not contain the conversion logic itself. It leverages a set of declared converters which are subclasses of ConverterHelper. A converter is a piece of code that handles:

  • either the conversion of a Representation to an object
  • or the conversion of an object to a Representation
  • or both conversion

Conversion mainly relies on the media type of the given Representation (application/json, text/xml, etc.) and an instance of a specific class. For example, the default converter provided by the core module (org.restlet.jar) allows the conversion of Web forms (media type “application/x-www-form-urlencoded”) to org.restlet.data.Form instances and vice versa. The converter provided the FreeMarker extension is only able to generate Representations from a FreeMarker Template (class freemarker.template.Template).

A converter is declared using a simple text file located in the “META-INF/services” source directory. Its name is “org.restlet.engine.converter.ConverterHelper” and it contains generally a single line of text which is the full path of the converter class. For example, the FreeMarker extension contains in the “src/META-INF/services” directory such text file with the following line of text: “org.restlet.ext.freemarker.FreemarkerConverter”.

Available converters

Conversion from representations to objects

ModuleFrom Representations with media typeTo Object
CoreAPPLICATION_JAVA_OBJECTjava.lang.Object
CoreAPPLICATION_JAVA_OBJECT_XMLjava.lang.Object
CoreAPPLICATION_WWW_FORMorg.restlet.Form
Coreany kind of Representationsjava.lang.String, java.io.InputStream, java.io.Reader, java.nio.ReadableByteChannel
AtomAPPLICATION_ATOMorg.restlet.ext.atom.Feed
AtomAPPLICATION_ATOM_PUBorg.restlet.ext.atom.Service
JacksonAPPLICATION_JSONan Object
JAXBAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XMLobject supporting JAXB annotations, org.restlet.ext.JaxbRepresentation
JiBXAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XMLJiBX bound object, org.restlet.ext.JibxRepresentation
JSONAPPLICATION_JSONorg.json.JSONArray, org.json.JSONObject, org.json.JSONTokener
RDFTEXT_RDF_N3, TEXT_RDF_NTRIPLES, APPLICATION_RDF_TURTLE,APPLICATION_ALL_XMLorg.restlet.ext.rdf.Graph
WADLAPPLICATION_WADLorg.restlet.ext.wadl.ApplicationInfo
XMLAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XMLorg.w3c.dom.Document, org.restlet.ext.xml.DomRepresentation, org.restlet.ext.xml.SaxRepresentation
XStreamAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XML, APPLICATION_JSON(requires Jettison dependency) java.lang.Object, org.restlet.ext.xstream.XStreamRepresentation

Conversion from objects to representations

ModuleFrom ObjectTo Representations with media type
Corejava.lang.String, java.io.File, java.io.InputStream, java.io.Reader, StringRepresentation, FileRepresentation, InputStreamRepresentation, ReaderRepresentation, org.restlet.representation.Representationany
Coreorg.restlet.FormAPPLICATION_WWW_FORM
Corejava.io.SerializableAPPLICATION_JAVA_OBJECT, APPLICATION_JAVA_OBJECT_XML
Atomorg.restlet.ext.atom.FeedAPPLICATION_ATOM
Atomorg.restlet.ext.atom.ServiceAPPLICATION_ATOM_PUB
FreeMarkerfreemarker.template.Templateany
Jacksonan ObjectAPPLICATION_JSON
JavaMaila javax.mail.Messagea org.restlet.ext.javamail.MessageRepresentation
JAXBobject supporting JAXB annotations, org.restlet.ext.JaxbRepresentationAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XML
JiBXJiBX bound object, org.restlet.ext.JibxRepresentationAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XML
JSONorg.json.JSONArray, org.json.JSONObject, org.json.JSONTokenerAPPLICATION_JSON
RDForg.restlet.ext.rdf.GraphTEXT_RDF_N3, TEXT_RDF_NTRIPLES, APPLICATION_RDF_TURTLE, APPLICATION_ALL_XML
ROMEcom.sun.syndication..fedd.synd.SyndFeedorg.restlet.ext.rome.SyndFeedRepresentation
Velocityorg.apache.velocity.Templateany
WADLorg.restlet.ext.wadl.ApplicationInfoAPPLICATION_WADL
XMLorg.w3c.dom.Document, org.restlet.ext.xml.DomRepresentation, org.restlet.ext.xml.SaxRepresentationAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XML
XStreaman objectAPPLICATION_ALL_XML, APPLICATION_XML, TEXT_XML, APPLICATION_JSON