DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Play in Action – Building REST Services

Play in Action – Building REST Services

Using the Play framework to create controllers and REST services.

Siva Prasad Rao Janapati user avatar by
Siva Prasad Rao Janapati
·
Apr. 07, 16 · Integration Zone · Code Snippet
Like (5)
Save
Tweet
4.75K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

public Result <action_name> (parameters)

Here, action returns play.mvc.Result. Play has 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 a different implementation of actions:

Image title

The routes file entry is given below:

Image title

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

Image title

The routes file entry is given below:

Image title

Now, we will see how the POST action is implemented:

Image title

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

Image title

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

Play REST Services

REST Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Pre-Commit Hooks DevOps Engineer Should Know To Control Kubernetes
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle
  • How to Perform Visual Regression Testing Using Cypress
  • Best Practices for Resource Management in PrestoDB

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo