Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use Play! to do it all. Writing REST services in Play is very very easy.</p> <p>Firstly, the routes file makes it straightforward to write routes that conform to the REST approach.</p> <p>Then, you write your actions, in the controller, for each API method you want to create.</p> <p>Depending on how you want to return the result (XML, JSON etc), there are a few methods you can use. For example, using the renderJSON method, allows the results to be rendered very easily. If you want to render XML, then you can just do so in the same way as you would build an HTML document in your View.</p> <p>Here is a neat example.</p> <p>routes file</p> <pre><code>GET /user/{id} Application.getUser(format:'xml') GET /user/{id}/json Application.getUserJSON POST /user/ Application.createUser PUT /user/{id} Application.updateUser DELETE /user/{id} Application.deleteUser </code></pre> <p>Application file</p> <pre><code>public static void createUser(User newUser) { newUser.save(); renderText("success"); } public static void updateUser(Long id, User user) { User dbUser = User.findById(id); dbUser.updateDetails(user); // some model logic you would write to do a safe merge dbUser.save(); renderText("success"); } public static void deleteUser(Long id) { // first check authority User.findById(id).delete(); renderText("success"); } public static void getUser(Long id) { User user = User.findById(id) renderJSON(user); } public static void getUserJSON(Long id) { User user = User.findById(id) renderJSON(user); } </code></pre> <p>getUser.xml file</p> <pre><code>&lt;user&gt; &lt;name&gt;${user.name}&lt;/name&gt; &lt;dob&gt;${user.dob}&lt;/dob&gt; .... etc etc &lt;/user&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload