views:

255

answers:

1

Since JAX-WS rely on JAXB, and since I observed the code that unpack the XML bean in JAX-B Reference Implementation, I guess the difference is not made and that a JAXWS client always return an empty collection, even the webservice result was a null element:

public T startPacking(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException {
        T collection = acc.get(bean);
        if(collection==null) {
            collection = ClassFactory.create(implClass);
            if(!acc.isAdapted())
                acc.set(bean,collection);
        }
        collection.clear();
        return collection;
    }

I agree that for best interoperability service contracts should be non ambiguous and avoid such differences, but it seems that the JAX-WS service I'm invoking (hosted on a Jboss server with Jbossws implementation) is always returning as expected a null empty collection (tested with SoapUI).

I used for my test code generated by wsimport. Return element is defined as:

@XmlElement(name = "return", nillable = true)
protected List<String> _return;

I even tested to change the Response class getReturn method from :

public List<String> getReturn() {
    if (_return == null) {
        _return = new ArrayList<String>();
    }
    return this._return;
}

to

public List<String> getReturn() {
    return this._return;
}

but without success.

Any helpful information/comment regarding this problem is welcome !

+3  A: 

There is no way to make a difference between an empty collection and null in the XML. A collection is usually serialized as a sequence of tags (xs:sequence in the schema), without an enclosing tag representing the collection itself.

<item value="item1"/>
<item value="item2"/>

I fear that whether you obtain null and an empty collection will be implementation specific, and I would not rely on it. If you need to make the distinction, you can wrap the collection into another object so as to generate an enclosing tag (I didn't found another way to have an enclosing tag be generated).

<items>
  <item value="item1"/>
  <item value="item2"/>
</items>
ewernli