A Java web service client that gets a list of objects

I just solved a problem with a Java web service client I've been working on. I've been trying to read a Java web service that was created with Apache Axis2, and it has methods that can return an array or List of User objects. I couldn't find any examples on the Axis2 web site that showed how to get an array or List from a web service client, but I finally find the solution by digging around a little.

In this post I'll provide some sample Java source code that shows what I did to solve this problem.

The Java web service client

First, here's the Java web service client that gets a list of User objects from a web service:

package sample.pojo.rpcclient;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import java.lang.reflect.*;
import java.util.*;
import sample.pojo.data.User;
import static java.lang.System.out;

public class UserClient
{

  public static void main(String[] foos) throws AxisFault {

      RPCServiceClient serviceClient = new RPCServiceClient();
      Options options = serviceClient.getOptions();
      EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/UserService");
      options.setTo(targetEPR);

      //
      // here i call a web service method that returns one User
      //
      QName qname1 = new QName("http://service.pojo.sample", "getUser");
      Object[] args1 = new Object[] { };
      Class[] types1 = new Class[] { User.class };
      Object[] response1 = serviceClient.invokeBlocking(qname1, args1, types1);
     
      User u1 = (User) response1[0];
      if (u1 == null) {
          System.out.println("u1 was null");
          return;
      }
     
      System.out.println(u1.getFirstName());
      System.out.println(u1.getLastName());
      System.out.println(u1.getUserId());

      //
      // the biggie:
      // call a web service method that returns a List of User objects
      //
      QName qname2 = new QName("http://service.pojo.sample", "getUserList");
      Object[] args2 = new Object[] { };
      Class[] types2 = new Class[] { User[].class };
      Object[] response2 = serviceClient.invokeBlocking(qname2,args2,types2);
      User[] users = (User[]) response2[0];

      for (int i=0; i<users.length; i++)
      {
          User u2 = users[i];
          if (u2 == null) {
              System.out.println("u2 was null");
              System.exit(1);
          }
          System.out.println(i + ": " + u2.getFirstName() + "," + u2.getLastName() + ", " + u2.getUserId());
      }
  }

}

As you might guess from looking at that sample code, the most important section of code is this block:

QName qname2 = new QName("http://service.pojo.sample", "getUserList");
Object[] args2 = new Object[] { };
Class[] types2 = new Class[] { User[].class };
Object[] response2 = serviceClient.invokeBlocking(qname2,args2,types2);
User[] users = (User[]) response2[0];

A lot of the magic to the solution is buried in those simple User[] references, where I'm saying that I expect to get an array of User objects back from my web service call.

The Java web service

Here's the code for my Java web service, including a getUserList method that returns a List of User objects:

package sample.pojo.service;

import java.util.ArrayList;
import java.util.List;
import sample.pojo.data.User;

// note: can call the service methods like this from a browser
// http://localhost:8080/axis2/services/UserService/setTwoNumbers?param0=5&param1=10
// http://localhost:8080/axis2/services/UserService/getNum1
// http://localhost:8080/axis2/services/UserService/getNum2

public class UserService
{
	User user;
	int num1, num2;
	
	public void setTwoNumbers(int num1, int num2)
	{
		this.num1 = num1;
		this.num2 = num2;
	}
	
	public int getNum1()
	{
		return num1;
	}
	
	public int getNum2()
	{
		return num2;
	}
	
	public void setUser(User user)
	{
     this.user = user;
	}

  public User getUser()
  {
	  User u = new User();
	  u.setFirstName("Barney");
	  u.setLastName("Rubble");
	  u.setUserId(99);
	  return u;
  }
  
  public List<User> getUserList()
  {
    List<User> users = new ArrayList<User>();
    User al = new User();
    al.setFirstName("Al");
    al.setLastName("Alexander");
    al.setUserId(100);
    users.add(al);

    User fred = new User();
    fred.setFirstName("Fred");
    fred.setLastName("Flinstone");
    fred.setUserId(101);
    users.add(fred);

    return users;
  }
  
  public User[] getUserArray()
  {
    User[] arr = new User[2];
    User al = new User();
    al.setFirstName("Al");
    al.setLastName("Alexander");
    al.setUserId(100);
    arr[0] = al;

    User fred = new User();
    fred.setFirstName("Fred");
    fred.setLastName("Flinstone");
    fred.setUserId(101);
    arr[1] = fred;
    return arr;
  }
}

The User class

And here's the Java source code for my User class:

package sample.pojo.data;

public class User
{
  private String firstName;
  private String lastName;
  private int userId;
  
  public String getFirstName()
  {
    return firstName;
  }
  public void setFirstName(String firstName)
  {
    this.firstName = firstName;
  }
  public String getLastName()
  {
    return lastName;
  }
  public void setLastName(String lastName)
  {
    this.lastName = lastName;
  }
  public int getUserId()
  {
    return userId;
  }
  public void setUserId(int userId)
  {
    this.userId = userId;
  }
  
}