Social Groups

We will create a simple soap web service with richer data types ( List and a user defined class) using wsimport we will generate the client side stubs.

Problem Statement

We will create an employee service that will return employee details based on employee numbers. We will have three methods

one that takes an employee number (int) and return an Employee object (Employee.java),

another method that takes a list of employee numbers and return a list of employee objects (List<Employee>) and

one that return all employee details. We will use a utility class to populate sample values.

Important considerations

When we use programmer defined types and collections we need to follow certain guidelines for correct results.

One such guideline that we follow here is: The JavaBean properties are of types String and int with getters and setters, and collections like List should have a toArray() method.

Package and import statements are not shown in examples, you can add them manually or an IDE like eclipse will do most of them for you.

We will not be using any server like Glassfish for this example, but will be using Java SE endpoint publisher. You can skip step 4 if you are deploying in an application server like Glassfish. You can then assume the wsdl url according to the service class name.

Step 1: Create the user defined class Employee.java

Create an employee class Employee.java with two fields – number and name.

Code:

public class Employee {

String empName;

int empNumber;

Employee(String name, int number) {

this.empName = name;

this.empNumber = number;

}

//You should give a default constructor

Employee() {}

//setters and getters should be present

}

Step 2: Create the utility class EmployeeUtils.java

Create a utility class EmployeeUtils.java that has following methods:

void createEmployees() that creates a sample list of employees.

Employee findEmployee(int empNum)

List<Employee> findEmployees(List<Integer> empNums)

List<Employee> getAllEmployees()

The createEmployees() method can be called by the constructor of SIB or EmployeeUtils itself, and findEmployees() and getAllEmployees() will be called by corresponding web methods in the SIB.

Code:

public class EmployeeUtils {

List<Employee> empList;

EmployeeUtils() {

empList = new ArrayList<Employee>();

createEmployees();

}

public void createEmployees() {

Employee emp1 = new Employee();

emp1.setEmpName("Sneha");

emp1.setEmpNumber(1);

empList.add(emp1);

Employee emp2 = new Employee();

emp2.setEmpName("Heartin");

emp2.setEmpNumber(2);

empList.add(emp2);

}

public Employee findEmployee(int empNum) {

for (Employee e : empList) {

if (e.getEmpNumber() == empNum)

return e;

}

return null;

}

public List<Employee> findEmployees(List<Integer> empNums) {

List<Employee> employeeList = new ArrayList<Employee>();

for (Integer i : empNums) {

employeeList.add(findEmployee(i));

}

return employeeList;

}

public List<Employee> getAllEmployees() {

return this.empList;

}

}

Step 3: Create SEI and SIB with 3 web methods:

Employee findEmployee(int empNum)

List<Employee> findEmployees(List<Integer> empNums)

List<Employee> getAllEmployees()

The findEmployees() and getAllEmployees() will call corresponding methods in the utility class and return values.

Step 5: Create the client support code using wsimport

This command will create a set of java files and their corresponding class files in client/generated folder which we can use for writing the client. Without –keep option only class files will be generated.

Step 6: Create the client file and execute

Before using wsimport, you needed to first pass the wsdl url into the URL constructor to create an URL object. Then you needed to create a QName object using QName constructor passing the service url and the service name. The Service.create() method accepts url and QName and then create a factory for the service of type Service. The service.getPort() then returns a reference to a java object of the SEI type. However using the wsimport generated files, we can easily get the port.

Code:

EmployeeServiceImplService service = new EmployeeServiceImplService();

EmployeeService port = service.getEmployeeServiceImplPort();

This is different from previous code because the url and QName details are there in the wsimport generated EmployeeServiceImplService.java class.

You are here

This is a personal technical blog where we share our understanding on various concepts and is neither an official page or documentation for the products described here, nor the official views of the companies we work with.

Keywords used in this website are trademarks of their respective owners. This website is not affiliated with Oracle™ and/or any of the JEE frameworks like Spring™, Struts™, Hibernate™ and JSF™.

All contents and materials are provided freely without any warranty or liability and nothing within the site should be considered as professional advice. In any doubt, please ask, and we will try to help you based on our knowledge. Please let us know if you feel anything is not right here (including any copyright violation) and we will act upon it as fast as we can.