You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Adam Gordon <ad...@gmail.com> on 2012/06/05 22:36:20 UTC

Empty vs. Null collection serialization issues in JAX-B over REST

This may be in the docs somewhere but I've missed it.  It may be a
configuration issue and I don't have it set.  The problem we're seeing is
that while our marshalling/unmarshalling unit tests are passing
successfully, it is not working correctly when deployed.

We have a bean with a List<String> field.  The class is annotated with:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)

It should be noted that our unit tests do not actually make a WebClient
call, rather use the Marshaller and Unmarshaller objects from JAXBContext.

We need to be able to discern between an empty collection and a null
collection.  Anyone know what we're doing wrong?  Thanks.

--adam

http://gordonizer.com

Re: Empty vs. Null collection serialization issues in JAX-B over REST

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 06/06/12 16:18, icfantv wrote:
> So, after a ton of trial and error, I think we've found the root issue and I
> can't find it documented anywhere.
>
> For fields that represent collections, it appears you need to add both the
> XmlElement("...") and XmlElementWrapper("...") attributes or empty
> collections will not be serialized and will result in the deserialized
> object version having a null value rather than empty.
>
> Can anyone speak as to why this is the case?  Thanks.
I think this is a JAXB issue which Dan is also referring to

Cheers, Sergey
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Empty-vs-Null-collection-serialization-issues-in-JAX-B-over-REST-tp5709155p5709207.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: Empty vs. Null collection serialization issues in JAX-B over REST

Posted by Daniel Kulp <dk...@apache.org>.
On Wednesday, June 06, 2012 08:18:27 AM icfantv wrote:
> So, after a ton of trial and error, I think we've found the root issue and
> I can't find it documented anywhere.
> 
> For fields that represent collections, it appears you need to add both the
> XmlElement("...") and XmlElementWrapper("...") attributes or empty
> collections will not be serialized and will result in the deserialized
> object version having a null value rather than empty.
> 
> Can anyone speak as to why this is the case?  Thanks.

Has to do with the generated XML and schema.   Without the wrapper, you 
would get something like:
<sequence name="...">
   <element name="item" minOccurs="0" maxOccurs="unbounded"/>
</sequence>

In that case, on the wire, you either have items written out or not.   There 
is no way on the wire to tell if, when there are no items, if it should be a 
null collection or empy collection.

With the wrapper, you get:
<sequence name="...">
   <element name="wrapper" minOccurs="0">
     <complexType>
        <sequence>
           <element name="item" minOccurs="0" maxOccurs="unbounded"/>
        </sequence>
     </complexType>
   </element>
</sequence>

Where the "wrapper" element represents the collection itself.  If no 
present, then the collection is null.  If it is present, the collection 
exists.  If there are no child items, then it is empty.

Dan




> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Empty-vs-Null-collection-serialization-is
> sues-in-JAX-B-over-REST-tp5709155p5709207.html Sent from the cxf-user
> mailing list archive at Nabble.com.
-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com


Re: Empty vs. Null collection serialization issues in JAX-B over REST

Posted by icfantv <ad...@gmail.com>.
So, after a ton of trial and error, I think we've found the root issue and I
can't find it documented anywhere.

For fields that represent collections, it appears you need to add both the
XmlElement("...") and XmlElementWrapper("...") attributes or empty
collections will not be serialized and will result in the deserialized
object version having a null value rather than empty.

Can anyone speak as to why this is the case?  Thanks.

--
View this message in context: http://cxf.547215.n5.nabble.com/Empty-vs-Null-collection-serialization-issues-in-JAX-B-over-REST-tp5709155p5709207.html
Sent from the cxf-user mailing list archive at Nabble.com.