Rest API with Play Framework using Java

Representational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. Web Services that conform to the REST architectural style, or RESTful web services, provide interoperability between computer systems on the internet. REST-compliant web services allow the requesting systems to access and manipulate textual representations of web resources by using a uniform and predefined set of stateless operations. Other kinds of web services, such as SOAP web services, expose their own arbitrary sets of operations.

Creating a new project with dependencies

Intellij IDEA lets you quickly create a Play application without using a command prompt. You don’t need to configure anything outside of the IDE, the SBT build tool takes care of downloading appropriate libraries, resolving dependencies and building the project.

For detailed steps refer:

https://www.playframework.com/documentation/2.6.x/IDE#intellij-idea

After creating the project, add the relevant dependencies. Refer to the link above.

Creating the Models and the Repositories

Model class is a class which represents data object which can be used for transferring data in java application. It encapsulates direct access to data in object and ensures all data in object is accessed via getter methods. The data values are set using setter methods.

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. The repositories contain the basic CRUD Operations.

Creating the Controller

Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate. All the APIs are written as a part of the Controllers.

A small example:

public class StudentController {
 private Student model;
 private StudentView view;

 public StudentController(Student model, StudentView view){
 this.model = model;
 this.view = view;
 }

 public void setStudentName(String name){
 model.setName(name); 
 }

 public String getStudentName(){
 return model.getName(); 
 }

 public void setStudentRollNo(String rollNo){
 model.setRollNo(rollNo); 
 }

 public String getStudentRollNo(){
 return model.getRollNo(); 
 }

 public void updateView(){ 
 view.printStudentDetails(model.getName(), model.getRollNo());
 } 
}

Simple examples of CRUD Operations in the controller:

CREATE:
public Result create() {
 JsonNode json = request().body().asJson();
 if (json == null){
 return badRequest(Util.createResponse(
 “Expecting Json data”, false));
 }
 Student student = StudentStore.getInstance().addStudent(
 (Student) Json.fromJson(json, Student.class));
 JsonNode jsonObject = Json.toJson(student);
 return created(Util.createResponse(jsonObject, true));
}
RETRIEVE:
public Result retrieve(int id) {
 Student student = StudentStore.getInstance().getStudent(id);
 if (student == null) {
 return notFound(Util.createResponse(
 “Student with id:” + id + “ not found”, false));
 }
 JsonNode jsonObjects = Json.toJson(student);
 return ok(Util.createResponse(jsonObjects, true));
}
UPDATE:
public Result update() {
 JsonNode json = request().body().asJson();
 if (json == null){
 return badRequest(Util.createResponse(
 “Expecting Json data”, false));
 }
 Student student = StudentStore.getInstance().updateStudent(
 (Student) Json.fromJson(json, Student.class));
 if (student == null) {
 return notFound(Util.createResponse(
 “Student not found”, false));
 }
 
 JsonNode jsonObject = Json.toJson(student);
 return ok(Util.createResponse(jsonObject, true));
}
DELETE:
public Result delete(int id) {
 boolean status = StudentStore.getInstance().deleteStudent(id);
 if (!status) {
 return notFound(Util.createResponse(
 “Student with id:” + id + “ not found”, false));
 }
 return ok(Util.createResponse(
 “Student with id:” + id + “ deleted”, true));
}

You don’t need to build anything special to expose a REST API — by default Play supports REST-ful web services by enabling the developer to match an HTTP verb and an endpoint with a function defined in a custom controller via a flexible ‘routes’ file.

The router is the component that translates each incoming HTTP request to an action call (a public method in a controller class).

An HTTP request is seen as an event by the MVC framework. This event contains two major pieces of information:

  • the request path (such as /student/4212, /student/create), including the query string.
  • the HTTP method (GET, POST, …).

Routes are defined in the conf/routes file, which is compiled.

Example of a routes file:

POST /:id controllers.StudentController.retrieve(id:Int)
POST / controllers.StudentController.create()
PUT / controllers.StudentController.update()
DELETE /:id controllers.StudentController.delete(id:Int)