Develop Webservice from WSDL
This document describes how to develop a webservice fast using a WSDL.
In the WSDL, you will provide input/output parameters for the webservice. With the webservice framework you can generate the EJB class from WSDL to incorporate the business logic.
Let’s take one sample scenario to build the webservice.
Webservice will take employees’ locations in the input and calculate total number of employees residing in each city/state.
Step 1: Define your Webservice Structure.
Input
—->ReportCat(City/State)
—->Employee(Cardinality 1..n)
—————->EmployeeId
—————->Name
—————->City
—————->State
—————->Country
Output
—->EmployeeCount(Cardinality 1..n)
—————->Location(City/State)
—————->No_of_Employees
Step 2: Create one EJB DC.
Click on Finish to create the EJB DC.
Step 3: Create one EAR DC to deploy EJB DC.
Go to component properties for the ear dc, provide deployment dependency for the ejb dc.
Step 4: Now create WSDL in your EJB DC. Right Click on the root folder for EJB DC, click on New->Other->Web services->WSDL.
Give a name to your WSDL, choose SOAP Binding as RPC literal and click on finish.
Open WSDL to define structure for the webservice.
Rename Operation name to getEmployeeList and hit enter.
Now add Input/output parameters to the webservice.
Input
—->ReportCat(City/State)
—->Employee(Cardinality 1..n)
—————->EmployeeId
—————->Name
—————->City
—————->State
—————->Country
Right Click on type column for getEmployeeListRequest and change its type to complex(set Type->New Type->Complex Type).
Click on EmployeeInput to open in Design View. Right Click->Click on Add element to add fields to the EmployeeInput type.
The structure should look as below:
Similarly create output structure.
Output
—->EmployeeCount(Cardinality 1..n)
—————->Location(City/State)
—————->No_of_Employees
Your final WSDL should look as below:
Step 5:
Right click on WSDL file->Webservices->Generate JAVA bean Skeleton
Click on Finish. You will find list of JAVA files created as below.
For each type you have defined in the WSDL, it will generate a JAVA class.
It will have one bean class and interface, which would be useful for writing the businesslogic.
So for the above JAVA files: WSEmployee is the interface file and WSEmployeeImplBean is the bean class.
Step 6: Go to the interface file and comment the Webservice declarations as below.
Copy Webservice annotations from this file and paste it in bean file as below.
Step 7: Now edit getEmployeeList method to add calculation logic for number of employees.
This is a sample code assuming the list is already sorted.
public org.example.wsemployee.EmployeeOutput getEmployeeList(org.example.wsemployee.EmployeeInput getEmployeeListRequest) {
EmployeeOutput employeeOutput=new EmployeeOutput();
ArrayList<EmployeeSummary> employeeSum = new ArrayList<EmployeeSummary>();
EmployeeSummary empSumobj = new EmployeeSummary();
int count=0,index=0;
if(getEmployeeListRequest.employeeData!=null)
{ int employeeSize= getEmployeeListRequest.employeeData.size();
//Check Report category S-For State C-For City
if(getEmployeeListRequest.reportCat.equals(“S”))
{
for(EmployeeDetails employeeDet:getEmployeeListRequest.employeeData)
{
index++;
if(employeeDet.state.equals(empSumobj.location)||empSumobj.location==null)
{
count = count + 1;
empSumobj.location=employeeDet.state;
}
else if(!employeeDet.state.equals(empSumobj.location))
{
empSumobj.noOfEmployees = count;
employeeSum.add(empSumobj);
empSumobj=new EmployeeSummary();
empSumobj.location=employeeDet.state;
count = 1;
}
if(index==employeeSize)
{
empSumobj.noOfEmployees = count;
employeeSum.add(empSumobj);
}
}
}
}
employeeOutput.employeeSummary=employeeSum;
return employeeOutput;
}
}
Step 8: Deploy the Ear file.
You can find your webservice in WSNavigator(http://host:port/webdynpro/dispatcher/sap.com/tc~esi~esp~wsnav~ui/WSNavigator).
Click on Next to test Webservice.
Click on Next to put test parameters.
Click on Next to execute Webservice. You can find the output in Results column.
You can find more information on help.sap.com at following location:
http://help.sap.com/saphelp_nwce711/helpdata/en/46/9357326110581de10000000a1553f7/frameset.htm