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.
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.)
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With