RESTful Web Service in JDeveloper 12c

Because the REST (Representational State Transfer) is lightweight, simple and flexible, many developers are building and exploring the RESTful Web Services, making it very popular, nowadays. According Roy Fielding, REST is an “architectural style” that basically exploits the existing technology and protocols of the Web. RESTful is typically used to refer to web services implementing the REST architecture.

In this post, you will learn how to build a RESTful Web Service using JDeveloper 12c (12.1.3). Download the sample application: JDevRESTApp.zip.

Create a Custom Application.
Set the Application Name as JDevRESTApp, Application Package Prefix as br.com.waslleysouza, and click Next.
Name the Project as Model, select EJB and Java as Project Features and click Next.
Change the Default Package to br.com.waslleysouza.model and click Finish.

Create the Employees Entity.
In the Applications window, right-click Model Project and choose New > From Gallery.
In the New Gallery dialog, choose Business Tier > EJB > Entities from Tables, and click OK.
In the Step 2 of Create Entities from Tables dialog, click Next.

restful-web-service-in-jdeveloper-12c1

In the Step 3, click Next.

restful-web-service-in-jdeveloper-12c2

In the Step 4, select database connection and click Next.

restful-web-service-in-jdeveloper-12c3

In the Step 5, select Employees Table and click Next.

restful-web-service-in-jdeveloper-12c4

In the Step 6, click Next.

restful-web-service-in-jdeveloper-12c5

In the Step 7, click Finish.

restful-web-service-in-jdeveloper-12c6

Open the Employees.java file and perform the following steps:

  • Annotate the class with @XmlRootElement.
  • I think there is some bug when JDev generates entities from table, because SALARY property is not annotated. Annotate it with @Column(name = “SALARY”).
  • Annotate the employeesList and employees properties with @XmlTransient. Annotate the getEmployeesList and getEmployees accessors too.

restful-web-service-in-jdeveloper-12c7

Create the Employees Session Bean.
In the Applications window, right-click Model Project and choose New > From Gallery.
In the New Gallery dialog, choose Business Tier > Session Bean, and click OK.
In the Create Session Bean dialog, change the EJB Name to EmployeesService, and click Next.

restful-web-service-in-jdeveloper-12c8

Uncheck all methods, and click Finish.

restful-web-service-in-jdeveloper-12c9

Add the following methods to EmployeesService, EmployeesServiceBean and EmployeesServiceLocal.

public Employees persistEmployee(Employees employee) {
  em.persist(employee);
  return employee;
}

public Employees mergeEmployee(Employees employee) {
  Employees currentEmployee = getEmployee(employee.getEmployeeId());
  if (employee.getEmail() != null) 
    currentEmployee.setEmail(employee.getEmail());
  if (employee.getFirstName() != null)
    currentEmployee.setFirstName(employee.getFirstName());
  if (employee.getLastName() != null)
    currentEmployee.setLastName(employee.getLastName());
  if (employee.getHireDate() != null)
    currentEmployee.setHireDate(employee.getHireDate());
  if (employee.getJobId() != null)
    currentEmployee.setJobId(employee.getJobId());
  if (employee.getPhoneNumber() != null)
    currentEmployee.setPhoneNumber(employee.getPhoneNumber());
  if (employee.getSalary() != null)
    currentEmployee.setSalary(employee.getSalary());
  if (employee.getCommissionPct() != null)
    currentEmployee.setCommissionPct(employee.getCommissionPct());
  return em.merge(employee);
}

public void removeEmployee(Employees employee) {
  employee = em.find(Employees.class, employee.getEmployeeId());
  em.remove(employee);
}

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public List getEmployees() {
  return em.createNamedQuery("Employees.findAll", Employees.class).getResultList();
}

public Employees getEmployee(Integer employeeId) {
  if (employeeId != null) {
    return em.find(Employees.class, employeeId);
  }
  return null;
}

Create a REST Web Service Project.
In the Step 1, click Next.
In the Step 2, change the Default Package to br.com.waslleysouza.webservice and click Finish.
Double-click the WebService Project, add Model Project as Dependent Project, and click OK.

restful-web-service-in-jdeveloper-12c10

Create the Employees RESTful Service.
In the Applications window, right-click Rest Project and choose New > From Gallery.
In the New Gallery dialog, choose General > Java Class, and click OK.
In the Create Java Class dialog, change the Name to EmployeesResource and click OK.
Copy the following code inside your class:

@Stateless
public class EmployeesResource {

  @EJB
  EmployeesServiceLocal employeesService;

  public EmployeesResource() {
    super();
  }

  public void create(Employees employee) {
    employeesService.persistEmployee(employee);
  }

  public void update(Employees employee) {
    employeesService.mergeEmployee(employee);
  }

  public void delete(int id) {
    Employees employee = getById(id);
    if (null != employee) {
      employeesService.removeEmployee(employee);
    }
  }

  public Employees getById(int id) {
    return employeesService.getEmployee(id);
  }

  public List findAll() {
    return employeesService.getEmployees();
  }
}

In the Applications window, right-click EmployeesResource class and choose Create RESTful Service.
Choose JAX-RS 2.0 Style and click Next.
Configure the RESTful Service and click Finish.

restful-web-service-in-jdeveloper-12c11

In the Return Type Warning dialog, click OK.

restful-web-service-in-jdeveloper-12c12

This is the result!

restful-web-service-in-jdeveloper-12c13

Done!
To test the RESTful Service, right-click class EmployeesResource and choose Test Web Service.
You may test each service operation using HTTP Analyzer.

restful-web-service-in-jdeveloper-12c14

restful-web-service-in-jdeveloper-12c15

Waslley Souza

Author: Waslley Souza

Consultor Oracle com foco em tecnologias Oracle Fusion Middleware e SOA. Certificado Oracle WebCenter Portal, Oracle ADF e Java.

4 thoughts on “RESTful Web Service in JDeveloper 12c”

  1. “right-click EmployeesService.java file and choose Create RESTful Service.”
    “To test the RESTful Service, right-click EmployeesService.java file and choose Test Web Service.”

    shouldn’t both be “EmployeesResource.java” ?

  2. Hi,
    Thank you so much for the post.
    Could you please explain how to deploy this application on standalone weblogic server?
    We want the service URLs to be used in some other MAF applications.

    1. Hi Saurabh!
      You will need to create a WAR Deployment Profile for your WebService Project and an EAR Deployment Profile for your JDevRESTApp application.
      Double-click WebService Project, go to Deployment and create a new WAR Deployment Profile. Use default settings.
      In the main menu, choose Application > Application Properties. Go to Deployment and create a new EAR Deployment Profile.
      In the Edit EAR Deployment Profile Properties dialog, go to Application Assembly, expand WebService.jpr node and mark the WAR Deployment Profile.
      Right-click JDevRESTApp application and choose Deploy > .
      You can deploy directly to a standalone weblogic server or generate an EAR file.

      Let me know if it helped you.

Leave a Reply

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