Dec 28, 2013

Create RESTful Service from Java Class

With JDeveloper 12c finally it is possible to create RESTful Services from a Java Class. The implementation is based on Jersey following the Java EE 6 specification JAX-RS. For testing JDeveloper includes a comprehensive Tooling. In general RESTful services are really popular for mobile applications, e.g. ADF Mobile.


Get started
Create a simple Java Class for your business domain object plus a Service Facade that you want to expose as a RESTful Service.
package enpit.sample.adf12c.pojorest.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
    
    private Long id;
    private String firstname;
    private String lastname;
    private Date hiredate;    
    private Long addressId;
    
    public Person() {
        super();
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setAddressId(Long addressId) {
        this.addressId = addressId;
    }

    public Long getAddressId() {
        return addressId;
    }
}

Make sure to annotate the class with the @XmlRootElement otherwise the REST Service will not work for this domain object.
Next create a simple service facade with some test data, e.g.
package enpit.sample.adf12c.pojorest.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class PersonService 
{     
private List persons;    
 private Person person;    
 public PersonService()
 {        
 super();        
 this.persons = new ArrayList();       
  for (long i = 0; i < 10; i++) 
{           
  Person p = new Person();  
           p.setId(i);            
 p.setFirstname("Firstname " + i);            
 p.setLastname("Last " + i);            
 p.setHiredate(new Date());            
 this.persons.add(p);       
  }         
this.person = this.persons.get(0);    
 }
    public List getPersons()
{      
   return this.persons; 
    }
    public void addPerson(Person person) 
{     
    System.out.println("add person " + person);   
      if(person != null)
{       
      getPersons().add(person);  
       }
     }  
  public Person getPerson()
{      
   return person; 
    }
 }
So far there is nothing special. 
Create RESTful Service with the help of JDeveloper 12c
Now comes the interesting part. Select the PersonService.java in the application navigator and
choose "Create RESTful Service…"
You will get a wizard to specify the RESTful Service interface. Set the desired root path. Choose the desired media types and the Methods you want to expose as REST Service
After finishing the wizard the following happens
package enpit.sample.adf12c.pojorest.model;
import java.util.ArrayList; 
import java.util.Date;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam; 
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("rest")
@Produces(value = { "application/json""application/xml" })
@Consumes(value = { "application/json""application/xml" }) 
public class PersonService
 {  
   private List persons;  
   private Person person;   
  public PersonService()
 {      
   super();      
   this.persons = new ArrayList()
{         
for (long i = 0; i < 10; i++)
 {       
      Person p = new Person();    
         p.setId(i);      
       p.setFirstname("Firstname " + i);    
         p.setLastname("Last " + i);     
        p.setHiredate(new Date());    
         this.persons.add(p);   
      }      
   this.person = this.persons.get(0);   
  }
    @GET   
  @Path("/persons")   
  public List getPersons()
{     
    return this.persons;     }
    @POST  
   @Path("/person")     
    public void addPerson(@FormParam("person"Person person)
 {
        System.out.println("add person " + person);    
     if(person != null)
{             getPersons().add(person);         }     } 
     @GET     
@Path("/person")  
   public Person getPerson(){  
       return person;     }
}
- web.xml is created. The Jersey Servlet is configured properly.

 

                                                                                                                                                              
Testing the RESTful Service in JDeveloper 12c
Jdeveloper is quite powerful. It lets you test the RESTful service right in the IDE.
Right-Click on the PersonService.java class and choose Run.
In background WebLogic Server 12c starts and the RESTFul Service gets deployed.
The WADL is kind of WSDL for RESTful Services. Clicking on the "Target Application WADL" URL brings you to the overview of the provided service operations.
You can test each Service Operation right from the IDE. Pressing the "Test" Button brings you to the HTTP Analyzer Screen.
Here you can choose the Method, change HTTP Headers (e.g. what kind of media type you want to accept).
Press "Send Request" to actually test the Service Operation. Make sure to change the view to "HTTP Content" not "WADL Structure" otherwise you want see the pretty formatted output on the right hand side.
Change the Accept-Header to application/json and send the request again. The output automatically will be in JSON format.
The HTTP Analyzer is even so good to parse the POST Parameter to create a good looking form for data entry

No comments:

Post a Comment