JDeveloper RESTful JPA EJB3

 

* JDeveloper 11.1.1.6

Create Project

Create Jersey User Library

Create a user library and add all Jersey lib files to it
* Download both Jersey zip bundle and jar bundle from Jersey download site:
– Jersey 1.17.1 ZIP bundle
– Jersey 1.17.1 JAR bundle
* Unzip zip bundle
* From JDeveloper, go to Tools > Manage Libraries > User > New…
– Library Name: jersey1.7-lib
* Add Jersey jars files to the library
* Also check Deploy by default checkbox

Create Project

* In JDev, got to New > Projects > Generic Project
– Project Name: jpaejb3Demo1
– Default package: rest.jpaejb3
* From Project Properties > Libraries and Classpath > Add Library…, Add jersey1.7-lib to the classpath
* Also need to add j2ee1.5 api libary to class path

Create a DB Connection

* Create an db connection named hr

Create Entity Bean

* Right click newly created project
* Select New > Business Tier > EJB > Entities from Tables
* Select DEPARTMENTS table
* Package name: rest.jpaejb3

Create EJB Diagram

* Right click newly created project
* Select New > Business Tier > EJB > EJB Diagram (JPA/EJB 3.0)
* Enter:
– Name: EJB3
– Package: rest.jpaejb3

* Drag and drop Departments.java to the diagram

Create Stateless Session Bean

* Drag a Component Palette > EJB Components > EJB Nodes > Session Bean to the diagram
* Enter:
– EJB Name: DeptSessionEJB
– Session Type: Stateless
– Transaction Type: Container
– Mapped Name: RESTfulApp-jpaejb3Demo1-DeptSessionEJB
– Checked: Generate Session Facade Methods
– Entity implementation: JPA Entities
– Persistence Unit: jpaejb3Demo1
– Bean class: rest.jpaejb3.DeptSessionEJBBean

Create JAX-RS

* Create a new Java class and annotate with Jersey annotations

package rest.jpaejb3;
 
import java.util.Hashtable;
 
import java.util.List;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
@Path("/deptService")
public class DeptService {
    private DeptSessionEJB deptBean;
    private String mappedName = 
        "RESTfulApp-jpaejb3Demo1-DeptSessionEJB#rest.jpaejb3.DeptSessionEJB";
 
    public DeptService() {
        Context ctx;
        try {
            ctx = getInitialContext();
            deptBean = (DeptSessionEJB)ctx
                .lookup(mappedName);;
        } catch (NamingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
 
    @GET
    @Path("{deptId}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Departments findDeptment(@PathParam("deptId") int deptId){
        return deptBean.findDeptById(deptId);
    }
 
    @GET
    @Path("/departments")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public List<Departments> listDepartments(){
        return deptBean.getDepartmentsFindAll();
    }
 
    private static Context getInitialContext() throws NamingException {
        return new InitialContext();
    }
}

* Click on @Path(“/deptService”), then click on the question mark yellow balloon, and select configure web.xml for Jersey JAX-WS web services

* Also annotate Departments class with @XMLRootElement

@Entity
@NamedQueries({
  @NamedQuery(name = "Departments.findAll", query = "select o from Departments o")
})
@XmlRootElement
public class Departments implements Serializable {

Deploy

* Right click DeptService and select Run
[04:50:38 PM] —- Deployment finished. —-
Run startup time: 3713 ms.
[Application RESTfulApp deployed to Server Instance IntegratedWebLogicServer]

Target Application WADL — http://localhost:7101/RESTfulApp-jpaejb3Demo1-context-root/jersey/application.wadl

Target URL — http://127.0.0.1:7101/RESTfulApp-jpaejb3Demo1-context-root/jersey/deptService/departments

* Test by pointing browser to http://127.0.0.1:7101/RESTfulApp-jpaejb3Demo1-context-root/jersey/deptService/departments and you should see sample output:

<departmentss><departments><departmentId>10</departmentId><departmentName>Administration</departmentName><locationId>1700</locationId><managerId>200</managerId></departments><departments><departmentId>20</departmentId><departmentName>Marketing</departmentName><locationId>1800</locationId><managerId>201</managerId></departments>

Issues

A message body writer for Java class java.util.Vector, and Java type java.util.List…

* A message body writer for Java class java.util.ArrayList
* How to produce JSON output with Jersey 1.17.1 using JAXB

References

* RESTful services with jQuery and Java using JAX-RS and Jersey
* Developing RESTful Web Services from JDeveloper 11g (11.1.1.4)
* RESTful Web Service using JPA/EJB 3.0 Entities
* http://www.oracle.com/webfolder/technetwork/tutorials/obe/jdev/obe11jdev/ps1/ejb/ejb.html
* Easy way to access JPA with REST (JSON / XML)

This entry was posted in java, restful and tagged , , . Bookmark the permalink.

One Response to JDeveloper RESTful JPA EJB3

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.