woensdag 2 maart 2011

How to create a web service client

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
I choose for a solution that:
  • Works for SOAP and RESTful web services
  • The location of the web service host can be changed while running the web service
Especially changing the host was an important criteria for me. For development I would like to use a development webservice and for production of course a production server.

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
A lot of files have been created in the project. The EmployeesWebPortClient class contains the entry point of calling the web service.

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)
Set the dependencies of the HrWebServiceProject, add RpcWebServiceProject.

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;

}

}

The EmployeeClient extends the HrWebServiceClient:

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 getAllEmployees() throws Exception {

EmployeesWebPortClient client = getClient();

Employee[] result = client.getAllEmployees(null).getReturn();

return convertToList(result);

}

}

For each method in the web service a method is created in the java class. For test purposes some code can be added so the web service client can be executed from JDeveloper:

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 result = new EmployeeClient(LOCALHOST).getAllEmployees();

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());

}

The result of this is:

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


Step 4

Create a deployment profile (JAR file) for the HrWebServiceProject and create the jar file. This file will be needed in the next blog ‘How to create web service based ADF pages’.

5 opmerkingen: