RESTful Web Services Exposition via Play 2.x framework and their access through an iOS app – Part 1/2

Introduction

This article is a two parts step-by-step tutorial on how to easily expose RESTful web services using Play 2.x Framework and consume them in an iOS app using AFNetworking.

In this first part, I will explain how to easily expose your apps datas in JSON format via a REST Web Service, using the Play 2.x framework.

In the second part to come, I will give you some details on how to access the web services through your iOS app.

Step by Step

Create your Play Application

I will suppose that you already have a running installation of Play 2.x framework on your machine. If you don’t, here is a link to the Play Framework documentation, which provides a great Getting Started tutorial : http://www.playframework.com/documentation/2.1.1/Home.

To create your Play Application, just run this command on your favorite folder:

$> play new helloWeb

Play will now ask you two questions:

What is the application name? [helloWeb]
>
Which template do you want to use for this new application?
    1 - Create a simple Scala application
    2 - Create a simple Java application
> 2

Just press Enter to answer the first question and type in 2 to the second to choose the Java template. Play will now say it better than me:

OK, application helloWeb is created.
Have fun!

Write your Web Service method

We are now going to write a simple method that returns a JSON result. Go to your controllers folder (./app/controllers) and open the Application.java file.

Application.java must contain its default definition which renders your index.html page:

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {
    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }
}

We are going to add our method, calling it helloWeb(). Just add the following method below index():

public static Result helloWeb() {
    ObjectNode result = Json.newObject();

    result.put("content", "Hello Web");

    return ok(result);
}

Here are the steps taken to create this simple method:

  • Create a new JSON ObjectNode called result
  • Put a String object "Hello Web" for the key "content"
  • Return result via the ok() method to associate it via a 200 HTTP Status Code

To make it work, we will need to add these two imports:

import org.codehaus.jackson.node.ObjectNode;
import play.libs.Json;

That’s it ! We created our Web Service method, now all we need is to expose it!

Expose your Web Service route

Last step : go to your conf folder (./app/conf) and open the routes file.

routes must contain its default definition which declares two routes, one for the Home page and another for all your assets:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET / controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

All we have to do now is to declare our Web Service’s route, just like this :

# Hello Web (JSON Web Service)
GET /helloWeb controllers.Application.helloWeb()

Done! You can access your newly created Web Service at its declared URL path. By default:

http://localhost:9000/helloWeb

Which should display :

{
    "content":"Hello Web"
}

Discussion

Now that you know how to expose web services, what about code organization? Where would you or do you put the WS declarations? One or several of your existing controllers? A specific one?

I would greatly like to have others opinion on this, so feel free to leave a comment.

2 thoughts on “RESTful Web Services Exposition via Play 2.x framework and their access through an iOS app – Part 1/2

  1. Hi
    I’m a junior developer in Scala. I am going to use restful service and JSON and play framework in may peroject, But I dont know how to apply restful. Please give me serveral simple examples about that.

    I have seen many example but what I need is that through a restful and by scala code with jason format and implement play, I can transaction with web application in simplest way.
    Thanks
    Shaho

Leave a Reply

Your email address will not be published. Required fields are marked *