Play in action – Building REST Services

Play Framework - Build Modern & Scalable Web Apps with Java and Scala

 

                       In this article will try to build REST services. To build REST services first we need to create controller and actions. In Play we can create controller by extending play.mvc.Controller. The methods in the controller which will take users request are called as “actions”. The action method signature is denoted as shown below.

public Result <action_name> (parameters)

Here, action returns play.mvc.Result. Play has below implementations for Result which we can return from the action method. Below are the out of the box implementations for Result.

ok --> HTTP status code is 200
notFound --> HTTP status code is 404 
badRequest --> HTTP status code is 400 
internalServerError --> HTTP status code is 500 
movedPermanently 301 --> HTTP status code is 301 
found  --> HTTP status code is 302
temporaryRedirect--> HTTP status code is 307
forbidden--> HTTP status code is 403
status --> Developer can return any HTTP status code

 Now, we will see different implementation of actions.

get student

The routes file entry is given below.

get student routes

We can send query parameters to the action. The implementation is explained below.

get student queryparam

The routes file entry is given below.

get student query param routes

Now, we will see how POST action is implemented.

save student

Now, let us see the routes file entry to handle above request.

save student routes

The source code created to explain the above example is available on GitHub. In the coming article we will see how to integrate Play with MySQL. Till then “Stay Hungry To Learn“.

Play REST Services
Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with:
Posted in Frameworks, Play

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: