Melanjutkan blog sebelumnya, kali ini kita akan membuat REST API menggunakan standar Java EE pada framework KumuluzEE.
- Tambahkan modul jax-rs untuk membuat REST
- Tambahkan modul cdi untuk context dependency injection
- Tambahkan modul jpa untuk ORM ke database
- Tambahkan library HSQLDB untuk database
- Tambahkan library Lombok
- Tambahkan directory META-INF pada directory resources, dan buat file persistence.xml
- Buat domain model Students
- Ubah main class App menjadi seperti dibawah ini
- Buat resource (REST) API untuk Students
- Buat file beans.xml pada directory META-INF untuk mengaktifkan context dependency injection
- Kemudian build menggunakan perintah Maven
- Jalankan aplikasi dengan perintah :
<dependency>
<groupId>com.kumuluz.ee</groupId>
<artifactId>kumuluzee-jax-rs</artifactId>
<version>${kumuluzee.version}</version>
</dependency>
<dependency>
<groupId>com.kumuluz.ee</groupId>
<artifactId>kumuluzee-cdi</artifactId>
<version>${kumuluzee.version}</version>
</dependency>
<dependency>
<groupId>com.kumuluz.ee</groupId>
<artifactId>kumuluzee-jpa</artifactId>
<version>${kumuluzee.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="schools" transaction-type="RESOURCE_LOCAL">
<class>org.nostra.kumuluz.models.Students</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:hsqldb:mem:unit-testing-jpa:9001:blog" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
</properties>
</persistence-unit>
</persistence>
@Data
@Entity
@NamedQuery(name="Students.findAll", query="SELECT s FROM Students s")
public class Students {
@Id
private String id;
private String firstName;
private String lastName;
private Date dob;
@PrePersist
public void prePersist(){
id = UUID.randomUUID().toString();
}
}
@ApplicationPath("/v1/")
public class App extends javax.ws.rs.core.Application {
}
@Path("/students")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RequestScoped
public class StudentsResource {
@PersistenceContext(unitName = "schools")
private EntityManager em;
@GET
public Response getStudents() {
TypedQuery query = em.createNamedQuery("Students.findAll", Students.class);
List studentses = query.getResultList();
return Response.ok(studentses).build();
}
@GET
@Path("/{id}")
public Response getStudents(@PathParam("id") String id) {
Students b = em.find(Students.class, id);
return Response.ok(b).build();
}
@POST
public Response createStudents(Students b) {
b.setId(null);
em.getTransaction().begin();
em.persist(b);
em.getTransaction().commit();
return Response.status(Response.Status.CREATED).entity(b).build();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_2.xsd"
bean-discovery-mode="annotated">
</beans>
mvn clean package
java -cp target/classes:target/dependency/* com.kumuluz.ee.EeApplication
Test REST API dengan cara :
- Call Http POST method ke http://localhost:8080/v1/students untuk insert data ke database
- Call Http GET method atau Buka browser dengan url http://localhost:8080/v1/students
- Call Http GET method atau Buka browser dengan url http://localhost:8080/v1/students/{id}
curl -X POST 'http://localhost:8080/v1/students' \
-H 'Content-Type:application/json' \
-d '{"firstName":"agus","lastName":"winarno","dob":"1984-08-17"}'
{
"id":"c8fe9c5f-3004-419d-b457-706f2ff02ac9",
"firstName":"agus",
"lastName":"winarno",
"dob":461548800000
}
Sekarang kita sudah dapat membuat microservice dengan Java EE API, semoga bermanfaat :)
Bitbucket link :
Referensi :
Bitbucket link :
Referensi :
No comments:
Post a Comment