12. Create Simple POST
At conf/routes, we add url for API access.
POST /person controllers.Application.addPerson()
The method is POST, the access to this API is
{baseurl}/person. For this example, we use json
format for body.
The process is on controllers/Application.java
method addPerson()
13. Create models
Create models/Person.java
package models;
import com.avaje.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Person extends Model{
@Id
public Long id;
public String name;
public static Finder<Integer, Person> find
= new Finder<Integer, Person>(Integer.class, Person.class);
}
14. Create controller
Add the method addPerson() at controller
application.java
@BodyParser.Of(BodyParser.Json.class)
public Result addPerson(){
JsonNode json = request().body().asJson();
Person person = Json.fromJson(json, Person.class);
if (person.toString().equals("")){
return badRequest("Missing parameter");
}
person.save();
return ok();
}
15. Let’s try
First create run configuration: Choose SBT Task and chose for the Task
is run.
Execute Run.
Because of the first
time server start, it
need trigger script
evolution execute first
by open the base url
on the browser and
apply the script.
16. Let’s try
Let curl from command line:
curl -X POST -H "Content-Type: application/json" -H "Cache-Control:
no-cache" -d '{
"name":"Faren"
}' 'http://localhost:9000/person’
It will store one object Person.
17. Create Simple GET
Add url on routes
GET /person controllers.Application.listPerson()
Add controller Application.java
public Result listPerson(){
List<Person> persons = new Model.Finder(String.class,
Person.class).all();
return ok(toJson(persons));
}
18. Let’s try
No need to Rerun the server. Just curl the GET
method from command line.
curl -X GET -H "Cache-Control: no-cache" 'http://localhost:9000/person’
The Output:
[{"id":1,"name":"Faren"},{"id":33,"name":"Faren"}]
19. Get single record
Add url on routes
GET /person/:id controllers.Application.getPerson(id: Int)
Add controller Application.java
public Result getPerson(int id){
Person person = Person.find.byId(id);
if (person == null){
return notFound("User not found!");
}
return ok(toJson(person));
}
20. Let’s try
Run curl on command line:
curl -X GET -H "Cache-Control: no-cache" 'http://localhost:9000/person/1’
The Output:
{
"id": 1,
"name": "Faren"
}
21. PUT
Add url on routes
PUT /person/:id controllers.Application.updatePerson(id: Int)
Add controller Application.java
@BodyParser.Of(BodyParser.Json.class)
public Result updatePerson(int id){
Person person = Person.find.byId(id);
if (person == null){
return notFound("User not found");
}
JsonNode json = request().body().asJson();
Person personToBe = Json.fromJson(json, Person.class);
person = personToBe;
person.update();
return ok();
}
22. DELETE
Add url on routes
DELETE /person/:id controllers.Application.deletePerson(id: Int)
Add controller Application.java
public Result deletePerson(int id){
Person person = Person.find.byId(id);
if (person == null){
return notFound("User not found");
}
person.delete();
return ok();
}
23. API with parameter
For example:
http://localhost:9000/searchperson?name=Faren
Add url on routes
GET /searchperson controllers.Application.searchPerson(name: String)
Add controller Application.java
public Result searchPerson(String name){
List<Person> persons = Person.find.where()
.like("name", "%"+name+"%")
.findList();
if (persons == null){
return notFound("User not found!");
}
return ok(toJson(persons));
}