Monday, December 21, 2015

ADF : Exposing BC Component using REST Service

In this blog, we are Exposing BC Component using Rest Service.

For example write service to get 5 Employees name from Employees table.
Use following steps:

Step 1: create ADF Application 
Step 2: Create EO and VO for Employee

Step 3:Create java class name Employee and 3 variable as :
    private Integer empId;
    private String empName;
    private String address;

and generate accessors for this

Step 4: Create java class for EmpWrapper ,In this class create  private List<Emplyee> lstEmp; variable to store Objects of Employee.

write @XmlRootElement above EmpWapper Class

Step 5: create java class EmpService
write  public EmpWapper getEmp() {}
In this method write bc4j  press Ctrl+Enter you will get follwing suggestion

Press Enter, code is generated to access AM instances

        ADFContext oldContext = ADFContext.initADFContext(null, null, null, null);
        try {
            String amDef = "test.TestModule"; 
            String config = "TestModuleLocal";
            ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
            ViewObject vo = am.findViewObject("TestView"); // VO to Access
            // Work with your appmodule and view object here
            Configuration.releaseRootApplicationModule(am, true);
        } finally {
            ADFContext.resetADFContext(oldContext);
        }

Add your logic to get 5 Employes name:

Copy and Paste following method code in Class


public EmpWapper getEmp() {
        EmpWapper objEmp=new EmpWapper();
       
        ADFContext oldContext = ADFContext.initADFContext(null, null, null, null);
        try {
            String amDef = "model.AppModule";
            String config = "AppModuleLocal";
            ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
            ViewObject vo = am.findViewObject("EmployeesView");
            int i=0;
            List<Emplyee> lstEmp=new ArrayList<Emplyee>();
            while(vo.hasNext()){
                
              Row row= vo.next();
               
              Emplyee emp=new Emplyee();
              emp.setAddress(row.getAttribute("Email").toString());
              emp.setEmpId(new Integer(row.getAttribute("EmployeeId").toString()));
              emp.setEmpName(row.getAttribute("FirstName").toString()); 
              lstEmp.add(emp);
              
               
             i++;
             if(i==5)
               break;
            }
            objEmp.setLstEmp(lstEmp);   
            // Work with your appmodule and view object here
            Configuration.releaseRootApplicationModule(am, true);
           
        } finally {
            ADFContext.resetADFContext(oldContext);
        }
        return objEmp;

    }

Step 6: right click on EmpService class and click on Create Restful Service

you will window like as follws:
Click on Produce and select application/xml media Type

Step 7: No win Configure HTTP method set

Type = Get
Produce = application/json
path =emp

and last finish.

then WebService is created . EmpService class looks like as follws:


Step 8:Now Deploye project and Test service

Output:

Note : suppose you will select in Step 7 :
Produce = application/xml

then output will be like this;

Hope this will help you

No comments:

Post a Comment