Thursday, September 11, 2008

It is not necessary to use wsimport generated POJO Files

When we have a Web Service in which we either pass the POJO object as a parameter or as a return type, by running wsimport command in order to generate Web Service Client similiar POJO classes are generated.
Suppose you have a Web Service with a Web Method that takes and returns a POJO of Class Employee:-
public class Employee {
 private int age;
 private String name;

 public Employee() {

 public Employee(int age, String name) {
  this.age = age; = name;
 //write getter setter for properties to comply to Java Bean/POJO standard
and the Web Method is:
 public Employee printEmployee(Employee e) {
  System.out.println(e.getName() + " - " + e.getAge());
  return e;
On generating the client, an Employee POJO is automatically generated but with some XML stuff and no constructor i.e. default constructor:
 @XmlType(name = "employee", propOrder = {"age", "name"})  
 public class Employee {  
  protected int age;  
  protected String name;  
  // Setters and Getters for the properties are also generated 
There seems to be two things about this generated POJO that irritates me:
  1. The newly generated Employee POJO has a lot of XML stuff that does not do any work in service invocation.
  2. I miss the constructor that takes in values for the POJO i.e. new Employee("name", 23);
Solution is simple:- Delete the generated POJO's from the client generated by wsimport command and copy the POJO's from the Web Service side to the generated client code. The package name of the POJO need not be the same on the Web Service and the Client side i.e. on the Web Service it can be pack.Employee where as in client it can be client.Employee. It works without any problems.

No comments:

Post a Comment