On Wed January 13 2010 4:51:17 am [email protected] wrote:
> Hello,
>
> I have a problem with the return type of a web service function:
>
> public List<Customer> findCustomer(String name);
>
> If my service returns a empty list, the list get null on the client. Is
> this a bug? Is there any workaround? Do I have to use Arrays instead of
> Lists?
Not really a bug. It's more due to how the spec maps the List into schema
and thus the representation on the wire. It would map that list to something
like:
<element name="return" type="tns:customer"
maxOccurs="unbounded" minOccurs="1"/>
Thus, if the list is empty or if the list is null, no "return" element is put
on the wire at all. On the receiving side, the runtime really has no
indication of whether it should be an empty list or a null list.
I think the only way around it would be to create a new bean like:
@XmlType(name = "findCustomerResponse")
public class FindCustomerResponse {
@XmlElementWrapper(nillable = true, name="return")
@XmlElement(name = "customers")
List<Customer> customers;
......getter/setters.....
}
and then add an annotation to the method:
@ResponseWrapper(class = "......FindCustomerResponse")
public List<Customer> findCustomer(String name);
That changes the schema a bit to create a single "return" element that is
nillable that then wrappers the "customer" elements.
--
Daniel Kulp
[email protected]
http://www.dankulp.com/blog