11. 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()
12. Create models
Create models/Person.java
package models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
public long id;
public String name;
}
13. Create controller
Add the method addPerson() at controller
application.java
@Transactional
@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");
}
JPA.em().persist(person);
return ok();
}
14. Let’s try
First create run configuration: Choose SBT Task
and chose for the Task is run.
Execute Run.
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.
15. Create Simple GET
Add url on routes
GET /person controllers.Application.listPerson()
Add controller Application.java
@Transactional(readOnly = true)
public Result listPerson(){
CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
CriteriaQuery<Person> all = cq.select(root);
TypedQuery<Person> allQuery = JPA.em().createQuery(all);
JsonNode jsonNode = toJson(allQuery.getResultList());
return ok(jsonNode);
}
16. 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":2,"name":"Dromdev"},{"id":3,"name":"inidevel
oper"}]
17. Get single record
Add url on routes
GET /person/:id controllers.Application.getPerson(id: Long)
Add controller Application.java
@Transactional
public Result getPerson(Long id){
Person person = JPA.em().find(Person.class, id);
if (person==null){
return notFound("User not found");
}
return ok(toJson(person));
}
18. 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"
}
19. PUT
Add url on routes
PUT /person/:id controllers.Application.updatePerson(id: Long)
Add controller Application.java
@Transactional
public Result updatePerson(Long id){
Person person = JPA.em().find(Person.class, id);
if (person==null){
return notFound("User not found");
}
JsonNode json= request().body().asJson();
Person personToBe = Json.fromJson(json, Person.class);
person.name = personToBe.name;
return ok();
}
20. DELETE
Add url on routes
DELETE /person/:id controllers.Application.deletePerson(id: Long)
Add controller Application.java
@Transactional
public Result deletePerson(Long id){
Person person = JPA.em().find(Person.class, id);
if (person==null){
return notFound("User not found");
}
JPA.em().remove(person);
return ok();
}
21. API with parameter
For example:
http://localhost:9000/searchperson?name=inideveloper
Add url on routes
GET /searchperson controllers.Application.searchPerson(name: String)
Add controller Application.java
@Transactional
public Result searchPerson(String name){
TypedQuery<Person> query = JPA.em().createQuery
("select p from Person p where p.name = :name", Person.class)
.setParameter("name", name);
try {
Person person = query.getSingleResult();
return ok(toJson(person));
}catch (NoResultException e){
return notFound("User not found");
}
}