Exposing Multiple Methods under One Resource In a Single URL Data Control

Previously, a URL Data Control could be associated with only one HTTP Method. So  if the developer had to expose multiple operations under  a single Resource, he/she had to create one DC for each operation.  Now since Jdeveloper  supports such a functionality from 11.1.2.2.0 version, multiple operations can be included in a single DC. This blog explains how to include all the operations exposed by a REST Service Resource in a single Data Control. We will use following REST Service to create URL Data Control.

import java.util.ArrayList;
import java.util.Iterator;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@Path("project1")
public class EmployeeService {
    private ArrayList<Employee> EmpList = new ArrayList<Employee>();

    public EmployeeService() {
        EmpList.add(new Employee("Adam", 1001, 35, "ADF"));
        EmpList.add(new Employee("Bob", 1002, 42, "HR"));
        EmpList.add(new Employee("George", 1003, 54, "Apps"));

    }

    @GET
    @Produces("application/xml")
    public ArrayList<Employee> getEmpList() {
        return EmpList;
    }

    @POST
    @Consumes("application/xml")
    @Produces("application/xml")
    public ArrayList<Employee> addEmployee(Employee emp) {
        EmpList.add(emp);
        return EmpList;
    }

    @DELETE
    @Consumes("text/plain")
    public ArrayList<Employee> deleteEmployee(@QueryParam("name")
                                              String name) {
        Iterator it = EmpList.iterator();
        while (it.hasNext()) {
            Employee tmp = (Employee) it.next();
            if (tmp.getName().equalsIgnoreCase(name))
                EmpList.remove(tmp);

        }
        return EmpList;
    }

}
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
    String name;
    int id;
    int age;
    String dept;
    
    public Employee() {
        super();
    }

    Employee(String name, int id, int age, String dept) {
        this.name= name;
        this.id=id;
        this.age=age;
        this.dept=dept;
        
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getDept() {
        return dept;
    }
}

Run the REST Service before commencing with Data Control. 

To begin with, create ADF Fusion Application. In Model project invoke create DC Wizard. Name your Data Control RESTDC and click on Next to create URL Connection.



In the next step, give a name to the URL connection, for example URLConn and specify localhost address in the URL Endpoint field.

Also, choose GET as HTTP Method and supply remaining part of REST Service URL in the source field.



In Step 4, choose XML as the data format from the drop down menu. Click Next. XSD will be auto generated by the jdeveloper. In step 5, click on Test URL Connection to check if the connection has been successfully generated.



Click on finish to complete the wizard. The Data Control for GET method in the REST Service has been created successfully and can be viewed under Data Controls Panel.




Now, we will add remaining HTTP Methods in the service to RESTDC. Invoke Create URL DC Wizard in the Model project. Give RESTDC (same name as the previous DC) in the Name field and choose URLConn from the drop- down. Select POST as the HTTP Method and supply the path to POST Operation in source field.



In the next step, specify the XSD which specifies the structure of the request content. Click Next.Choose XML Data format from the drop-down and complete the wizard. You can see another operation added to the same Data Control in the DC Panel.




Similarly invoke Create URL DC Wizard to add DELETE Method to RESTDC.



In step 3, you will be asked to enter a value to test whether URL and source are valid. Enter  "abc" in the values field against param.



Choose XML as Data format and complete the wizard.

Now the Data Controls Panel has all three operations GET, POST and DELETE which are in the resource under a single URL DC.



The names of these methods (loadData, loadData1 etc) can be changed from DataControls.dcx file.


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today