views:

78

answers:

2

Hi, What is the right way to return empty arrays from webservices in Java? I need that empty arrays sholdn't be nulls on client. If I use Apache CXF or Axis 2 with default databinding I receive null insted of empty arrays. If I use CXF and AegisDatabinding -- problem is solved, but when I calling this webservice from Axis 2 client -- I receive null parameters on server (like in this question http://stackoverflow.com/questions/2087465/web-service-call-via-cxf-gives-null-parameters).

Thanks.

A: 

You can return array with one special element to differenciate it as Empty array.
Like it the array is of long then return

Long[] lngArray = new Long[1];
lngArray[0]= -1l;
org.life.java
A: 

This has been discussed a couple times on the CXF lists and JIRA:

http://cxf.547215.n5.nabble.com/CXF-2627-still-failign-in-2-2-9-td1247184.html#a1247184

https://issues.apache.org/jira/browse/CXF-2978

https://issues.apache.org/jira/browse/CXF-2627

The only way is to write custom wrapper objects that add the @XmlElementWrapper annotation.

Daniel Kulp
I marked getter with @XmlElementWrapper(nillable = true) and received array with one not initialized element insted of empty array. Reсeive this is more strange than receive null :)
chardex