I have been building REST services using Spring and Jersy. Both the frameworks are good to implement REST services. But, I was looking for a lightweight web framework to build REST services by considering some characteristics.
Play satisfies the required characteristics as shown below,
- REST Support
- Play has first in class support for REST.
- More Developer Productive
- Play provides high developer productivity but having hot reload of code, templates, config changes etc… Developer just need to do the code change and refresh the page to see the update. No re build and re deployment.
- Open source
- Play is open source and is released under the Apache 2 License.
- Less System Resource Utilization
- Play consumes less system resources like CPU and memory as it uses only one thread to process the requests(against to the traditional servers where one thread per request).
- Less Learning Curve
- Play is very simple to set up and work.
- Pluggable Architecture
- Play has numerous modules which you can plugin based on the requirement..
Now we will create our first Play with Java project by following below steps.
Step 1: Here Iam using Play version is 2.4.2. To work with this version, make sure that the JAVA_HOME environment variable is set to JDK8 or more.
set JAVA_HOME=<Path to JDK 8 or more where it installed> for Windows export JAVA_HOME=<Path to JDK 8 more where it installed> for Linux based
Step 2: Download the Typesafe’s Activator(Tool from Typesafe which will allow the developers to create Play project quickly. It is built on top of SBT). The Activator will be available in two formats. One is minimum downloadable artifact online version and the other one is offline fully downloadable version(suitable for developers with no internet connection). In this tutorial we are going to use minimum online version.
Step 3: Extract the downloaded Activator zip file and add it to PATH environment variable.
set PATH=<Path to extracted activator folder>;%PATH%; for Windows export PATH=<Path to extracted activator folder>:$PATH: for Linux based For example,set PATH=D:\My_Learnings\typesafe-activator-1.3.5-minimal\activator-1.3.5-minimal;%PATH%;
Step 4: Now, from the command prompt use the below command to create Play for Java application. There are other options available for activator. To know that, execute activator help command.
activator new Learning_Play_App play-java
Step 5: Now run the Play app which we created in Step 4 and see the default Play UI. From the command prompt, move to the project which we created and execute the below command. When we run the Activator, it will launch the Netty server. To stop the server, use ctrl+D
D:\My_Learnings>cd Learning_Play_App D:\My_Learnings\Learning_Play_App>activator run
First time when you run activator, it will download all the dependencies from the central repository and will take some time to start the Play application. After the application is started on default port 9000, open the browser and send the request to http://localhost:9999 to see the default Play UI as shown below.
You are done with your first Play Application creation. In the next article, we will try to understand the Play folder structure and exporting it as our favorite IDE project(Like Eclipse, IntelliJ etc..).