Developer notes written down before they get lost.
Restlet Extension for Piriti
Restlet is a RESTful Web framework for Java. There’s also a GWT edition available. The Piriti
Restlet extensions is built on top of that edition. Therefore the 2.x version of Restlet is used. There are two
representations available which use the Piriti readers to convert JSON and XML data to your model (POJOs and/or
GXT models).
Read and convert JSON
To read the JSON data you need an instance of JsonReader:
12345678
publicclassBook{interfaceBookReaderextendsJsonReader<Book>{}publicstaticfinalBookReaderJSON=GWT.create(BookReader.class);// Fields annotated with @JsonField...}
Then you can read the JSON data like this:
12345678910111213141516171819202122232425
ClientResourceclientResource=newClientResource("/resource/with/json/representation");clientResource.setOnResponse(newUniform(){@Overridepublicvoidhandle(Requestrequest,Responseresponse){PiritiJsonRepresentation<Book>representation=newPiritiJsonRepresentation<Book>(Book.JSON,response.getEntity());try{// Depending whether there's one book or an array of books// in your JSON dataList<Book>books=representation.getModels();Bookbook=representation.getModel();}catch(IOExceptione){e.printStackTrace();}}});clientResource.get(MediaType.APPLICATION_JSON);
The entity returned by the resource has to be a valid JSON object. In case you want to read a list of books, the
JSON object has to contain one key (name does not matter) with the array of books:
To read the XML data you need an instance of XmlReader
12345678
publicclassBook{interfaceBookReaderextendsXmlReader<Book>{}publicstaticfinalBookReaderXML=GWT.create(BookReader.class);// Fields annotated with @XmlField...}
Then you can read the XML data like this:
12345678910111213141516171819202122232425
ClientResourceclientResource=newClientResource("/resource/with/xml/representation");clientResource.setOnResponse(newUniform(){@Overridepublicvoidhandle(Requestrequest,Responseresponse){PiritiXmlRepresentation<Book>representation=newPiritiXmlRepresentation<Book>(Book.XML,response.getEntity());try{// Depending whether there's one book element or a list of book// elements in your XML dataList<Book>books=representation.getModels();Bookbook=representation.getModel();}catch(IOExceptione){e.printStackTrace();}}});clientResource.get(MediaType.TEXT_XML);
The entity returned by the resource has to be a valid XML document. In case you want to read a list of books, the
document must contain a list of book elements as direct children of the root element: