10

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.

4
  • is the list in the response message XML? Commented Jul 7, 2011 at 11:25
  • does it work with List instead of ArrayList? Commented Jul 7, 2011 at 11:26
  • 1
    Thilo - I tried having a List in my interface definition. However, you cannot declare an interface as a return type using a web service. Commented Jul 7, 2011 at 11:36
  • 1
    If you are fan of Collection then you can achieve that by declaring an ArrayList variable using a separate class.or you can go with array Commented Jul 7, 2011 at 11:42

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

Wow .. I've wondered why code generated by wsdl2java always wrapped collections in a trivial bean. In original JAX-WS bug java.net/jira/browse/JAX_WS-28 it also mentions using doc/literal style as a work around.
Not sure why this didnt work for me, put the ArrayList in a pojo, then changed to a List instead of ArrayList and while the objects returned from the WSDL generation now contain everything needed to populate the array (they were missing previously), and I can see before calling the web service that they're populated - on the receiving server side, they're still null
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.