Play Framework – XML API with Play Framework

We  will perceive how to just uncover a XML API with the Play Framework.

URL of Play! Are Restful basically, so it gets to be not difficult to make a little XML alongside the Web interface of Play Application.We should perceive how to do it. How about we take the illustration of a music library. Our model incorporates collections, craftsmen and sorts. The Album class resembles this:


 public class Album augments Model {
 public String name;
 @manytoone(cascade = {cascadetype.persist })
 public craftsman;
 public Date releasedate;
 public Genre

Kind is a straightforward Enum characterized as:

public enum Genre {
world – pop – rock –hiphop

We need to characterize a URL that returns a rundown of the collections in XML for a given kind, on a GET call.

To do this we must change the courses record:

GET/site/collections/{genre}  App.list
GET/collections/{genre}  App.listxml(format:'XML')

The main line compares to the HTML page (not demonstrated in this post) that displays a rundown of accessible collections: the arrangement is not specified, so render will be made with a HTML page.

In the 2nd line, the parameter (form: ‘xml’) demonstrates that the render() system for the controller will search for a document named listxml.xml. The{genre} parameter will be recouped in the URL and went to the controller.

For this situation we may add parameters to the html form later, without needing to effect the XML rendering,

So, I favored a division of the rendering in two unique techniques.

See the code of the Application.listxml() technique :

public static void listxml(string type) {
Type genreenum = tim.valueof(tim.storing().fasterrinners());
List albums= Album.find("time",union).fetch();

We are simply searching for the collection relating to the class parameter, and we ask for the rendering of the rundown. By the way we perceive how utilizing JPA with Play! is basic. The report will be made in the document comparing to the example {name of the controller method} + {.xml}.

For this situation it .

This layout, put in application/sees index, is characterized as takes after:

#{list collections, as:'album'}<album>
<release-date>${album.releasedate.format('zzzz')}</discharge date>

That is sufficient to uncover our collections in XML. By taking after the URL example characterized in the courses record, for instance by calling http://localhost:9000/collections/rock, we get the accompanying result:

<release-date>1991</discharge date>
<name>origin of Symmetry</name>
<release-date>2001</discharge date>
<name>green point invention </name>
<release-date>2006</discharge date>

Presently we should perceive how to send XML substance to add collections to our music library.

We need to send the accompanying substance, utilizing POST with application/xml substance sort :

<album><artist>metallica</artist><name>death Magnetic</name>
<release-date>2008</discharge date>

We add this line to the courses document to permit POST on/collection URL:

POST/collection  Application.savexml


Save xml system recovers the substance of the appeal in request. Body variable at that point it parses the substance to make a collection and spare it in the database. We utilize a class named play. libs.xpath to search the XML report :

public static void savexml(){
Documentbuilderfactory plant =filedirectory.newsentence();
Record = invalid;
attempt {
/make xml record
Documentbuilder manufacturer = factory.newdocumentbuilder();
record = builder.parse(requestbody);
} get (Exception e) {
Component albumnode = document.getdocumentelement();
/get the craftsman
String artistname = Xway.selecttext("musicion",framenode);
Craftsman = new Artist(artistname);
/get the name
String albumname = Xpath.selecttext("name", albumnode);
Collection = new Album(albumname);
/get the date
String date = Xpath.selecttext("release-date",albumnode);
Dateformat = new Simpledateformat("yyyy");
attempt {
album.releasedate = filestruct.par(time);
} get (Parseexception e) {
/get the class
String classification = Xpath.selecttext("genre", albumnode);
album.genre = genreenum;
/spare in db
album.artist = craftsman;;