Enterprise Integration Zone is brought to you in partnership with:

James is an open source developer working on Play framework. His passion is making web development simpler, through using the right tools in the right way for the right job. James is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

How to Write a REST API in Play Framework

  • submit to reddit

A very common question that we get on the Play mailing list is how do you write a REST API using Play Framework? There's no explicit documentation on it, you won't find a page in the Play documentation titled "Writing REST APIs". The question is often met with confusion, to those that try to answer it, the question for them is "how can you not write a REST API with Play? Play is all about REST."

So let me explain why we don't have a page on writing REST APIs. Play is fundamentally a framework for writing REST APIs, just like a fridge is a tool that is fundamentally for keeping food cold. When you buy a fridge, and you get the manual for a fridge, do you find a page titled "How to keep food cold using the fridge"? Probably not. You'll find instructions for installing the frige, turning it on, setting the temperature, adjusting the shelves, but you won't find instructions that explicitly say how to keep the food cold. Why not? Because it's assumed that you understand, when you buy the fridge, that the way to keep food cool in it is by putting food in and closing the door. The whole manual is about how to keep food cold, since that's the fridges fundamental function.

It's the same with Play. We assume first of all that you know what a REST API is. There's plenty of documentation out there on the web on what a REST API is, there's no reason for us to repeat this in our documentation, a good place to start might be this StackOverflow question. As the first answer to that question says, "Really, what it's about is using the true potential of HTTP", Play also provides everything you need to use the true potential of HTTP.

So we have documentation on writing routes in Scala and Java, we have documentation on sending results in Scala and Java, we have documentation on handling JSON in Scala and Java, and so on and so on. All this documentation is giving you the tools you need to implement what Play fundamentally about, that is, HTTP, which when realised to its true potential, will be REST. There's nothing special about a REST API in Play, writing a REST API in Play means writing a web application in the way that Play is designed to be used. We could probably rename the Play documentation home page to be "Writing a REST API in Play", that would accurately describe what most of the Play documentation is about.

Let me repeat again, Play is all about realising the full potential of HTTP, which means Play is all about REST. You want to read about how to write a REST API in Play? Read the Play documentation, it's all about writing a REST API in Play.

Published at DZone with permission of James Roper, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Luca Cavagnoli replied on Mon, 2013/06/17 - 3:33am

People would prefer to read one, straightforward "How to write REST API in Play" article, rather than reading documentation on writing routes in Scala and Java, documentation on sending results in Scala and Java,  documentation on handling JSON in Scala and Java, and so on and so on... and put all the pieces together. Just for the sake of saving time, you know.

Giovanni Cândid... replied on Sat, 2014/02/08 - 4:05pm

You have a point, but the default implementation of routes is tedious to write Restfull APIS. For every controller (C) there is the need to declare all routes for each Action (A) and Method (M) So, we and up in the worst case with C*A*4 redundant declarations. In other frameworks this is a common problem, so, we have better ways to declare routes.  I'm new to play, but experienced with PHP frameworks, Java EE and Spring, and both offer ways to declare restful routes in more scalable and productive ways. Well, the route can be override, but the point I wanna say is that this is a problem the framework could provide good, native, flexible, solution.   

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.