AngelsandDemons
AngelsandDemons

Reputation: 2843

Passing ArrayList in Web Service Call in Java

I have an ArrayList containing 10 values.

ArrayList<String> a=new ArrayList<String>();
a.add("a1");
a.add("a2");
a.add("a3");

I am invoking WebService to pass this ArrayList as an input parameter.. However when I created a dummy client and tested for the ArrayList I am not getting the output.. I am using Apache Axis 2. I realised that ArrayList is not synchornized and hence there may be a problem...How do I overcome this issue.... I am looking for the simplest solution... Can anyone please guide me...

The server side code is:-

public class TestWebSerrviceForArrayList
{

    public String personIdentity="RandomName";
    ArrayList<String> personArray=new ArrayList<String>();

    public ArrayList<String> addValuesArrayList()
    {
        personArray.add("Name1");
        personArray.add("Name2");
        personArray.add("Name3");
        personArray.add("Name4");
        System.out.println("Added values in person Array   "   +  personArray);
        return personArray;

    }

    public String getName()
    {
        return personIdentity;
    }

}

The code snippet for Client Side is :-

TestWebSerrviceForArrayListStub testWeb=new TestWebSerrviceForArrayListStub();
AddValuesArrayListResponse r=testWeb.(I AM NOT ABLE TO FIND THE FUNCTION NAME)

Upvotes: 1

Views: 10005

Answers (1)

Buhake Sindi
Buhake Sindi

Reputation: 89169

ArrayList is not understood by the Web Service, but List is. It's got nothing to do with synchronization of ArrayList but to match the data types understood by the WSDL.

As stated earlier: I would change the return type for this method to List<String> as follows (and regenerate the WSDL).

public List<String> addValuesArrayList()
    {
        personArray.add("Name1");
        personArray.add("Name2");
        personArray.add("Name3");
        personArray.add("Name4");
        System.out.println("Added values in person Array   "   +  personArray);
        return personArray;

    }

Upvotes: 1

Related Questions