Friday, September 18, 2015

RESTFul web service dengan RESTEasy

Apa yang dibutuhkan untuk membuat API web service berbasis Restful?
Jika kita menggunakan java banyak sekali framework yang bisa digunakan untuk membuat Restful dari web application. Contohnya : Apache CFX, JAX-RS, Jersey, Spring MVC.
Yang paling populer menurut saya adalah Spring MVC. Karena  Spring Framework sudah cukup lengkap. Sudah menyediakan banyak fitur, seperti dependency injection, ORM support, MVC dan lain lain.
Tapi kalau kita hanya ingin membuat webservice API yang skalanya kecil. Menurut saya agak cukup rumit karena library dependency yang cukup banyak apalagi harus configurasi juga di ApplicationContext xml.
Ada sebuah library java yang biasa digunakan untuk membangun web application yang bisa mengekspose RestFull webservice API.
Kita akan mencoba Menggunakan RESTEasy. Library ini termasuk dalam JBoss project. Aplikasi kita tidak harus dijalankan diatas JBoss App server, bisa dijalankan di app server yang lain seperti (tomcat, jetty, dan lainnya). Sudah memenuhi standart spesifikasi JAX-RS.
Dokumentasinya ada di sini : https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/index.html
Intinya JAX-RS membantu kita untuk melakukan mapping java class (POJO) menjadi web resource
JAX-RS memiliki beberapa standar annotasi seperti berikut ini :
  • @Path specifies the relative path for a resource class or method.
  • @GET, @PUT, @POST, @DELETE and @HEAD specify the HTTP request type of a resource.
  • @Produces specifies the response Internet media type
  • @Consumes specifies the accepted request Internet media types.
Secara garis besar kita hanya membuat class java yang diberikan anotasi jax-rs dan didaftarkan di web.xml

Tambahkan maven dependency
 <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-jaxrs</artifactId>  
       <version>3.0.4.Final</version>  
     </dependency>  
     <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-jackson-provider</artifactId>  
       <version>3.0.4.Final</version>  
     </dependency>  

Sebagai contoh buat class pojo yang akan kita ekspos sebagai JSON di api kita
 public class Employee extends Base {  
   private String name;  
   private String email;  
   private String phone;  
   private String address;  
   public Employee() { }  
   public Employee(Integer id, String name, String email, String phone, String address) {  
     this.id = id;  
     this.name = name;  
     this.email = email;  
     this.phone = phone;  
     this.address = address;  
   }  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
   public String getEmail() {  
     return email;  
   }  
   public void setEmail(String email) {  
     this.email = email;  
   }  
   public String getPhone() {  
     return phone;  
   }  
   public void setPhone(String phone) {  
     this.phone = phone;  
   }  
   public String getAddress() {  
     return address;  
   }  
   public void setAddress(String address) {  
     this.address = address;  
   }  
 }  

Buat class service employee yang bertugas untuk mengekpose endpoint yang kita perlukan. Misalnya kita akan membuat fungsi add, find, delete employee.
 import javax.ws.rs.*;  
 import javax.ws.rs.core.Response;  
 import java.util.ArrayList;  
 import java.util.List;  
 import java.util.logging.Logger;  
 @Path("/person")  
 public class EmployeeService {  
   private static final Logger LOGGER = Logger.getLogger(EmployeeService.class.getSimpleName());  
   private List<Employee> listEmployee = new ArrayList<Employee>();  
   public EmployeeService(){  
     listEmployee.add(new Employee(1, "budi", "budi@gmail.com", "081901444", "jakarta"));  
   }  
   @Path("/")  
   @Consumes("application/json")  
   @Produces("application/json")  
   @PUT  
   public Response addEmployee(Employee employee){  
     listEmployee.add(employee);  
     return Response.status(200).entity("new employee id "+employee.getId()).build();  
   }  
   @Path("/")  
   @Produces("application/json")  
   @GET  
   public Response findAllEmployee(){  
     return Response.status(200).entity(listEmployee).build();  
   }  
   @Path("/{id}")  
   @Produces("application/json")  
   @GET  
   public Response findById(@PathParam("id") Integer id){  
     Employee employee = listEmployee.get(id);  
     if(employee != null) return Response.status(200).entity(employee).build();  
     return Response.status(Response.Status.NOT_FOUND).build();  
   }  
   @Path("/{id}")  
   @Produces("application/json")  
   @DELETE  
   public Response DeleteById(@PathParam("id") int id){  
     Employee result = listEmployee.remove(id);  
     if(result != null) return Response.status(200).build();  
     return Response.status(Response.Status.NOT_FOUND).build();  
   }  
 }  

Register class service employes tadi ke dalam class jax-rs application (misalnya kita beri nama RestApplication).
 import javax.ws.rs.core.Application;  
 import java.util.HashSet;  
 import java.util.Set;  
 
 public class RestApplication extends Application {  
   private Set<Object> singletons = new HashSet<Object>();  
   public RestApplication() {  
     singletons.add(new EmployeeService());  
   }  
   @Override  
   public Set<Object> getSingletons() {  
     return singletons;  
   }  
 }  

Daftar class application tadi ke web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
   <!-- restEasy servlet config -->  
   <servlet-mapping>  
     <servlet-name>resteasy-servlet</servlet-name>  
     <url-pattern>/rest/*</url-pattern>  
   </servlet-mapping>  
   <!-- this should be the same URL pattern as the servlet-mapping property -->  
   <context-param>  
     <param-name>resteasy.servlet.mapping.prefix</param-name>  
     <param-value>/rest</param-value>  
   </context-param>  
   <servlet>  
     <servlet-name>resteasy-servlet</servlet-name>  
     <servlet-class>  
       org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
     </servlet-class>  
     <init-param>  
       <param-name>javax.ws.rs.Application</param-name>  
       <param-value>com.nostratech.gcm.notification.rest.RestApplication</param-value>  
     </init-param>  
   </servlet>  
   <session-config>  
     <session-timeout>  
       30  
     </session-timeout>  
   </session-config>  
 </web-app>  

Kita run web application kita (bisa dideploy diatas application server).

- GET /person/0
 {  
   "id": 1,  
   "version": null,  
   "name": "budi",  
   "email": "budi@gmail.com",  
   "phone": "081901444",  
   "address": "jakarta"  
 }  

- PUT /person/  (application/json)
 {  
   "id": 2,  
   "name": "andri",  
   "email": "andri@gmail.com",  
   "phone": "08190166",  
   "address": "bandung"  
 }  

- GET /person/

Referensi :
http://resteasy.jboss.org/ dan http://resteasy.jboss.org/docs
jax-rs spesifications : https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/index.html
source code (web app project yang menggunakan RestEasy) : https://bitbucket.org/andri_khrisharyadi/sample-gcm-sever

Terima kasih dan selamat belajar dan mencoba :)






No comments:

Post a Comment