Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an ArrayList from a WebService in Java

I am having a problem returning an ArrayList from my web service (Java).

I have written a test web service and client which consumes it. All appears to work fine - that is the client is calling the server and the server receives the operation request.

However, I have written a simple method that I want it to return an ArrayList.

I have my interface definition as follows:

@WebService
@SOAPBinding(style = Style.RPC)
public interface ISQLServerConnectionWS {

    @WebMethod
    ArrayList getSimpleArrayList();
}

I have my server side implementation to return the ArrayList:

@WebService(endpointInterface="WebServices.ISQLServerConnectionWS")
public class SQLConnectionWSServer
    implements ISQLServerConnectionWS {

    @Override
    public ArrayList getSimpleArrayList() {
        ArrayList al = new ArrayList();
        al.add( "This" );
        al.add( "is" );
        al.add( "a" );
        al.add( "test" );
        return al;
    }
}

And finally my client call to it:

ArrayList results = server.getSimpleArrayList();

The server populates the array list fine. However, back at the client side, the ArrayList is empty. It has a size of 0.

If I examine the WSDL at my URL (http://127.0.0.1:9876/myservice-sql?wsdl) for the executeSelectSQL, it looks like:

<message name="executeSelectSQLResponse">
    <part name="return" type="tns:arrayList"/>
</message>

Am I missing something obvious?

Edit:

However, if I have a web method defines in the interface as:

@WebMethod
String getAString();

and the server implementation:

@Override
public String getAString() {
    return "hello there";
}

then this works fine - "hello there" is received on the client.

like image 834
Andez Avatar asked Jul 07 '11 11:07

Andez


People also ask

How do you return an ArrayList array in Java?

To convert ArrayList to array in Java, we can use the toArray(T[] a) method of the ArrayList class. It will return an array containing all of the elements in this list in the proper order (from first to last element.)

How do you return a list directly in Java?

The list() method of java. util. Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

How do you return an ArrayList as a string?

To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method.

How do I return a copy of an ArrayList?

The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object.


2 Answers

Use an array instead of an ArrayList as JAXB cannot handle collections as top-level objects, only as properties of beans. Alternatively, create a bean and put the ArrayList in it.

See bug: JAXB-223: JAXB doesn't support collection classes as top level objects

like image 114
dogbane Avatar answered Oct 15 '22 23:10

dogbane


I will suggest create separate pojo class where declare a vaiable

private ArrayList ListData;

create setter/getter method and use the POJO class in your main class to set the arraylist. At same time the Operation getSimpleArrayList change the return type to that of POJO type. Accordingly change the wsdl file too.

like image 39
BOSS Avatar answered Oct 15 '22 23:10

BOSS