Insights >Blog

Getting started with the Play Framework (Java)

Daniel Bernal


December 19th, 2018

What is Play?

Play is a lightweight open source framework developed by Lightbend. It is written in Scala and therefore supports it, as well as Java. Companies such as LinkedIn, Coursera, Verizon and many more already work with Play.

Why is Play good to work with?

Play aims to make it easy for developers to work and optimize their productivity by using convention over configuration, hot code reloading, and by displaying application errors over the browser.

Some of the benefits of the Play framework are:

  • Hot code reloading. When developers make a change, they simply have to reload the browser to see it. This enables developers to work faster and more efficiently.
  • Asynchronous. Built on Netty, this makes I/O work without any blocker, with independent threads. Its compatibility with Akka, also makes it possible to create a fully asynchronous application.
  • Open source. The whole project is visible to developers and changes may be accepted if errors or bugs are solved. This also applies for framework improvements.
  • Error representation. Errors are shown in a clear, effective way. Play shows exactly the place where the error happened, including the file, line, and column. Errors are shown this way in compilation and runtime.
  • Flexibility. The Play framework is flexible. It is possible to configure the entire environment and more frameworks can be plugged in.
  • Constant updates. The Play framework is always updating and you can keep track of this on their website. Play is compatible with many of the recent useful technology stacks such as JSON, ORM, SQL and NoSQL.
  • Languages support. The Play framework is built on Scala but also supports Java, so there’s the chance to program on either one of these languages, or both.

How to create a small dummy application using Play

In order to learn a bit more about this framework, we would like to create a small dummy application. This will let us know more about the configuration environment and how the framework looks like.

We need to have set-up the following tools before beginning to work on our project:

  • SBT in order to build the application.
  • IntelliJ is going to be our preferred IDE.
  • Java 1.8 or greater for the source code.

To create a project you just need to open the IDE and go to Create New Project.

A pop-up will open, and there we search for Java Enterprise, select your preferred SDK and the Java EE Version, then scroll down and select Play 2.x or the specific version of Play framework you want to work with.

Click Next and give a name to your project and a location where you want to save it. In this case, we are creating a project to manage books and author information, so Library is the name we would like to give.

The project will be automatically created and loaded using SBT as the building tool as follows:

The project structure is the following:

  • app: Folder that contains the source code of the application
  • conf: Folder that contains the configuration of the application. The file application.conf contains the application related configurations. The file routes contains all the REST service endpoints and the source code which handle each of them.
  • project: folder contains build.properties and plugins.sbt where you can mention plugin related details.
  • public: contains all the static assets like Javascript, images, and CSS.
  • test: contains all the unit tests.
  • build.sbt file contains all the dependencies.

To validate that the project is stable, we are going to create a basic endpoint. In the controllers folder we are going to modify the class Application with the following code:

package controllers;
 
import play.mvc.Controller;
import play.mvc.Result;
 
public class Application extends Controller {
 
public Result health() {
return ok("Server is Up.");
}
 
}

Inside the routes file we are going to write the following code:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
 
# Home page
GET / controllers.Application.health()

To test the endpoint and application, on console or terminal we are going to execute the following command:

#sbt run

After some seconds we are going to see on the console output the following line:

info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

This means that our server has been deployed and we can test using the browser the next URL: localhost:9000, and we expect to see the following message:

So here we have already created a basic Java application (REST API) implemented with the Play framework.

I hope this helps you get started with Play! In my next post, I will explain how to conduct the implementation of the business model, which will require more code layers.

Share

Related posts

See also

Services

Software development

Software testing

Consultancy & innovation

User experience

Industries

Fintech

Media & entertainment

Healthcare

All industries

Insights

Blog

Whitepapers

Webinars

Videos

Why Belatrix?

International presence

Nearshore advantages

Project governance

Agile expertise

Flexible engagement models

Our talent development