Tuesday, March 15, 2016

Implementasi microservice dengan Java EE pada KumuluzEE

Melanjutkan blog sebelumnya, kali ini kita akan membuat REST API menggunakan standar Java EE pada framework KumuluzEE.

  1. Tambahkan modul jax-rs untuk membuat REST
  2. <dependency>
        <groupId>com.kumuluz.ee</groupId>
        <artifactId>kumuluzee-jax-rs</artifactId>
        <version>${kumuluzee.version}</version>
    </dependency>
  3. Tambahkan modul cdi untuk context dependency injection
  4. <dependency>
        <groupId>com.kumuluz.ee</groupId>
        <artifactId>kumuluzee-cdi</artifactId>
        <version>${kumuluzee.version}</version>
    </dependency>
  5. Tambahkan modul jpa untuk ORM ke database
  6. <dependency>
        <groupId>com.kumuluz.ee</groupId>
        <artifactId>kumuluzee-jpa</artifactId>
        <version>${kumuluzee.version}</version>
    </dependency>
  7. Tambahkan library HSQLDB untuk database
  8. <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.2</version>
    </dependency>
  9. Tambahkan library Lombok
  10. <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
    </dependency>
  11. Tambahkan directory META-INF pada directory resources, dan buat file persistence.xml
  12. <?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>
  13. Buat domain model Students
  14. @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();
        }
    }
    
  15. Ubah main class App menjadi seperti dibawah ini
  16. @ApplicationPath("/v1/")
    public class App extends javax.ws.rs.core.Application {
    }
    
  17. Buat resource (REST) API untuk Students
  18. @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();
        }
    }
    
  19. Buat file beans.xml pada directory META-INF untuk mengaktifkan context dependency injection
  20. <?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>
  21. Kemudian build menggunakan perintah Maven
  22. mvn clean package
  23. Jalankan aplikasi dengan perintah :
  24. java -cp target/classes:target/dependency/* com.kumuluz.ee.EeApplication

Test REST API dengan cara :
  1. Call Http POST method ke http://localhost:8080/v1/students untuk insert data ke database
  2. 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
    }
  3. Call Http GET method atau Buka browser dengan url http://localhost:8080/v1/students
  4. Call Http GET method atau Buka browser dengan url http://localhost:8080/v1/students/{id}
Sekarang kita sudah dapat membuat microservice dengan Java EE API, semoga bermanfaat :)
Bitbucket link :
Referensi :

No comments:

Post a Comment