[Lab] Soap Web Service with Collection Types and Generating Client Support Code using wsimport

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.

Code:

EmployeeService.java

@WebService

@SOAPBinding(style=Style.DOCUMENT,use=Use.LITERAL, parameterStyle=ParameterStyle.WRAPPED)

public interface EmployeeService

{

         @WebMethod

         public Employee findEmployee(int empNum);

         @WebMethod

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

         @WebMethod

         public List<Employee> getAllEmployees();

}

 

EmployeeServiceImpl.java

@WebService(endpointInterface = "com.javajee.ws.example4.EmployeeService")

public class EmployeeServiceImpl implements EmployeeService {

 

  EmployeeUtils empUtils = new EmployeeUtils();

 

  public Employee findEmployee(int empNum) {

    return empUtils.findEmployee(empNum);

  }

 

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

    return empUtils.findEmployees(empNums);

  }

 

  public List<Employee> getAllEmployees() {

    return empUtils.getAllEmployees();

  }

}

 

Step 4: Create the publisher and execute

You can skip this step if you are deploying in an application server like Glassfish. You can then assume the wsdl url according to the service class name.

The wsdl and xsd generated are attached at the end.

Code:

public class EmployeeServicePublisher {

 

  public static void main(String[] args)

    {

      Endpoint endpoint = Endpoint.create(new EmployeeServiceImpl());

      endpoint.publish("http://127.0.0.1:8888/ex4");

    }

}

 

Step 5: Create the client support code using wsimport

wsimport -keep -p client.generated http://127.0.0.1:8888/ex4?wsdl

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.

Files generated in my case are package-info.java, ObjectFactory.java, Employee.java, EmployeeService.java, EmployeeServiceImpleService.java. FindEmployee.java, FindEmployeeResponse.java, FindEmployees.java, FindEmployeesResponse.java, GetAllEmployees.java, GetAllEmployeesResponse.java and their corresponsing .class files.

 

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.

Previous code (without using wsimport):

URL url = new URL("http://127.0.0.1:8888/ts?wsdl");

QName qname = new

QName("http://tsexample1.ws.javajee.com/","TimeServiceImplService");

Service service = Service.create(url,qname);

TimeService port = service.getPort(TimeService.class);

 

Complete Client Code (with package and import statements):

package client;

 

import client.generated.*;

import java.util.List;

import java.util.ArrayList;

 

public class EmployeeServiceClient {

  public static void main(String[] args) {

    EmployeeServiceImplService service = new EmployeeServiceImplService();

    EmployeeService port = service.getEmployeeServiceImplPort();

 

    System.out.println("\nAll Employees:");

    List<Employee> allEmps = port.getAllEmployees();

    for (Employee e : allEmps) {

      System.out.println("Emp Number= " + e.getEmpNumber() + ". Emp Name= "

          + e.getEmpName());

    }

 

    System.out.println("\nFind employee with empnumber:");

    Employee emp = port.findEmployee(2);

    System.out.println("Emp Number= " + emp.getEmpNumber() + ". Emp Name= "

        + emp.getEmpName());

 

    System.out.println("\nAll Employees From a List:");

    List<Integer> inputList = new ArrayList<Integer>();

    inputList.add(1);

    inputList.add(2);

    List<Employee> selectEmps = port.findEmployees(inputList);

    for (Employee e : selectEmps) {

      System.out.println("Emp Number= " + e.getEmpNumber() + ". Emp Name= "

          + e.getEmpName());

    }

  }

}

 

The client executed fine with expected output:

 

Summary Of steps

  • Step 1: Create the user defined class Employee.java

  • Step 2: Create the utility class EmployeeUtils.java

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

  • Step 4: Create the publisher and execute

  • Step 5: Create the client support code using wsimport

  • Step 6: Create the client file and execute

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)