Technology: ADF11g
Developed in: JDeveloper 11.1.1.3.0
Used database schema: none
Summary
In this blog a solution is provided how to create a web service client. This blog is part of a sequence of 3 blogs:- How to create a RESTful web service that returns objects
- How to create a web service client
- How to create web service based ADF pages
- Works for SOAP and RESTful web services
- The location of the web service host can be changed while running the web service
Step 1
Create a new generic application in JDeveloper, call the project RpcWebServiceProject:
Step 2
On the project choose New – Web Service Proxy (under Business Tier – Web Services).
Choose:
- Client Style: JAX-RPC Web Logic Style
- Copy the URL or the location of the WSDL file
- For this example we use: http://localhost:7101/HrWebServices-HrWebServicesProject-context-root/EmployeesWebPort?WSDL
- Specify a package name:
- For example: nl.hr.demo.webservice.client.employees
- Leave all other settings to their default
To make maintenance easier create a new project where a new entry point for each web service is created. If the web service ever changes then the sources in the RpcWebServiceProject project can be regenerated (right click on the EmployeesWebServiceProxy – Regenerate web service) and know custom code get lost.
Step 3
Create a new project HrWebServiceProject containing two java classes:- EmployeeClient (package nl.hr.demo.webservices.client)
- HrWebServiceClient (package nl.hr.demo.webservices)
The HrWebServiceClient class is a generic class that creates the WSDL URL for given server location and contains methods to convert data types:
package nl.hr.demo.webservices;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class HrWebServiceClient {
private String webserviceBaseURL;
public HrWebServiceClient() {
super();
}
public HrWebServiceClient(String webserviceBaseURL) {
this.webserviceBaseURL = webserviceBaseURL;
}
protected String getWebserviceUrlAsString(String webservice) {
return webserviceBaseURL + "/" + webservice;
}
protected URL getWebserviceUrl(String webservice) {
URL wsdlLocationURL = null;
try {
wsdlLocationURL = new URL(getWebserviceUrlAsString(webservice));
} catch (MalformedURLException e) {
return null;
}
return wsdlLocationURL;
}
protected List convertToList(Object[] input) {
List list = new ArrayList();
for (int i = 0; i < input.length; i++) {
list.add(input[i]);
}
return list;
}
}
package nl.hr.demo.webservices.client;
import java.util.List;
import nl.hr.demo.webserivce.client.employees.EmployeesWebPortClient;
import nl.hr.demo.webservices.HrWebServiceClient;
import nl.hr.demo.webservices.Employee;
public class EmployeeClient extends HrWebServiceClient {
private static final String WSDL_NAME = "HrWebServices-HrWebServicesProject-context-root/EmployeesWebPort?WSDL";
public EmployeeClient() {
super();
}
public EmployeeClient(String webserviceBaseURL) {
super(webserviceBaseURL);
}
private EmployeesWebPortClient getClient() throws Exception {
EmployeesWebPortClient client = new EmployeesWebPortClient();
client.setPortCredentialProviderList();
client.setEndpoint(getWebserviceUrlAsString(WSDL_NAME));
return client;
}
public Employee getEmployeeById(int id) throws Exception {
EmployeesWebPortClient client = getClient();
return client.getEmployeeById(id);
}
public List
EmployeesWebPortClient client = getClient();
Employee[] result = client.getAllEmployees(null).getReturn();
return convertToList(result);
}
}
public static void main(String[] args) {
String LOCALHOST = "http://localhost:7101";
try {
System.err.println("Search employee ID 100");
Employee emp = new EmployeeClient(LOCALHOST).getEmployeeById(100);
print(emp);
System.err.println("Search employee ID 101");
emp = new EmployeeClient(LOCALHOST).getEmployeeById(101);
print(emp);
System.err.println("Search employee ID 102");
emp = new EmployeeClient(LOCALHOST).getEmployeeById(102);
print(emp);
System.err.println("Search all employees");
List
for (int i = 0; i < result.size(); i++) {
print(result.get(i));
}
if (result.size() == 0) {
System.err.println("niets terug");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void print(Employee employee) {
System.err.println(employee.getEmployeeId() + ": " +
employee.getFirstName() + " " +
employee.getLastName());
}
Search employee ID 100
100: Steven King
Search employee ID 101
101: Neena Kochhar
Search employee ID 102
102: Lex De Haan
Search all employees
100: Steven King
101: Neena Kochhar
102: Lex De Haan