Play Framework – Working on JSON

In case that you are a Java developer then you really needs to try play framework out. It is truly refreshing to take a few minutes, venture out of the takes fling universe of customary Java web application development and into something present day and function. I need to walk you through an extremely basic exercise where we will fabricate a web application with Play Framework. The application will used with JPA for diligence and lay open access to the information through a JSON over HTTP (hyper text transfer protocol) interface. The customer side of the application will be manufactured. We should begin.

Step 1)  Download and introduce Play Framework.

Step 2)  Create another Play application from the command line and afterward move to the new registry:

Play new playbars

Disc playbars

Step 3) If you want to work in an IDE then used to step here:

Play idealize

Play eclipsify

I want run however the IDE specific steps here, yet you can allow playing authentication on IDE mix if you need to get that setup.

Step 4) Begging the Play server from the new project file:

Step 6) lets begin by making another JPA substance. Keeping it exceptionally straightforward, let’s make another application-

Package models;
import play.db.jpa.model;
import javax.persistence.entity;
@Entity
open class Bar augments Model {
open String name;
}

This substance utilizes the standard JPA Entity annotation additionally amplifies the Play Model class which gives some pleasant comforts which you can read all the more about in the Play JPA docs. I’m utilizing simply a plain open property on this class however you can likewise utilize the Java Bean getter/setter stuff in you like.

play come -%time

The start Play in time mode which will subsequently recompile any progressions we make to the application. No bundling, redeploying, or restarting servers will be required.

Step 5) Open the application in your program:

The default page furnishes you with incredible Play documentation on the best way to begin. Anyway we will soon be changing that page. If you need to view the documentation by regional standards after we change the page you can utilize these connections:

* Documentation

* Play Javadoc

Step 7) Create a basic unit/ coordination test for this Bar question by making a “test/Bartest.java” record with the accompanying substance:

import org.junit.*;
import java.util.*;
import play.test.*;
import models.*;
open class Bartest develops Unittest {
@test
open void integrationtest() {
Bar = new Bar();
bar.name = "another bar";
bar.save();
assertnotnull(bar.id);
asserttrue(bar.findall().size() >= 1);
bar.delete();
asserttrue(bar.findall().size() == 0);
}
}

Step 8) Setup the default database by altering the “con/app.con” file and commenting the accompanying line:

# bf=men

These set up the delinquency directory to be an in-memory database.

Step 9) Move the application’s tests by opening the accompanying URL in your program:

Select “BarTime” and afterward click the “Start!” catch. The time should pass.

Step 10) Add the accompanying two systems to the “application/controllers/Application.java” record:

open static void addbar(bar bar) {
bar.save();
file();
}
open static void listbars() {
renderjson(bar.findall());
}

The addbar system takes a Bar, spares it, and afterward redirects over to the record page. Play will naturally parse demand parameters and populate the Bar object. The listbars system questions for the majority of the Bars in the database and after that yields then as serialized JSON.

Step 11) Play has an extremely adaptable model calculating to controllers. Then setup two new subjects that will delineate to the new strategies we have include to the Application controller. Add the successive lines to the “con/subjects” document yet verify you either include them over the “Get all” or essentially evacuate the “Get all” subject.

The primary subject hold HTTP COST solicitations to the “/” URL and holds them with the Application.addbar system. The second subject holds appeals to the “/bars.json” URL and holds them with the Application.listbars strategy.