You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTCINW)" <dk...@att.com> on 2009/12/19 20:49:40 UTC

serializing subclass of base class gets base class name in output

I had originally had a "Foo" class in my REST server which was
serializing fine as "<foo>".  I then realized that this was really a
base class, so I added the subclasses of it, which are "Bar" and "Gork".
The declaration of the list of these is the following:

    @XmlElementWrapper(name = "childFoos")
    @XmlElement(name = "foo")
    private List<Foo>   childFoos;

When I serialize this, I get exactly what I specified here, such that
each "Foo" element is a "foo".

What I want is to have a "<childFoos>" list, but each element will
either be "<bar>" or "<gork>", depending on the subclass.  The data I
have in each "foo" is correct, reflecting whether it was a "bar" or a
"gork", but I want the wrapper to change based on that.

I tried to remove the "XmlElement" wrapper here, but that just resulted
in "<childFoos>" wrapping a list of "<childFoos>", not a mixture of
"<bar>" or "<gork>" elements.

RE: serializing subclass of base class gets base class name in output

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozk@progress.com]
> Sent: Saturday, December 19, 2009 12:05 PM
> To: users@cxf.apache.org
> Subject: RE: serializing subclass of base class gets base class name
in
> output
> 
> Hi
> 
> You may want to try @XmlElements listing Bar.class and Gork.class
> instead of @XmlElement(name = "foo"), let us know please if it helps

On my first try this didn't work, because I only specified the "name"
attribute of each nested @XmlElement.  Once I added the "type" value
(which I had assumed it would figure out), it worked fine.

> -----Original Message-----
> From: KARR, DAVID (ATTCINW) [mailto:dk068x@att.com]
> Sent: 19 December 2009 19:50
> To: users@cxf.apache.org
> Subject: serializing subclass of base class gets base class name in
> output
> 
> I had originally had a "Foo" class in my REST server which was
> serializing fine as "<foo>".  I then realized that this was really a
> base class, so I added the subclasses of it, which are "Bar" and
> "Gork".
> The declaration of the list of these is the following:
> 
>     @XmlElementWrapper(name = "childFoos")
>     @XmlElement(name = "foo")
>     private List<Foo>   childFoos;
> 
> When I serialize this, I get exactly what I specified here, such that
> each "Foo" element is a "foo".
> 
> What I want is to have a "<childFoos>" list, but each element will
> either be "<bar>" or "<gork>", depending on the subclass.  The data I
> have in each "foo" is correct, reflecting whether it was a "bar" or a
> "gork", but I want the wrapper to change based on that.
> 
> I tried to remove the "XmlElement" wrapper here, but that just
resulted
> in "<childFoos>" wrapping a list of "<childFoos>", not a mixture of
> "<bar>" or "<gork>" elements.

RE: serializing subclass of base class gets base class name in output

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozk@progress.com]
> Sent: Saturday, December 19, 2009 12:05 PM
> To: users@cxf.apache.org
> Subject: RE: serializing subclass of base class gets base class name
in
> output
> 
> Hi
> 
> You may want to try @XmlElements listing Bar.class and Gork.class
> instead of @XmlElement(name = "foo"), let us know please if it helps

I assume that it would look something like this:

    @XmlElementWrapper(name = "childFoos")
//    @XmlElement(name = "foo")
    @XmlElements(value =
                 {@XmlElement(name = "bar"),
                  @XmlElement(name = "gork")})
    private List<Foo>   childFoos;

I hope this is wrong, because it didn't do what I had hoped.
Originally, I had all the "foo" elements wrapped with "<foo>", although
they were actually subclasses of Foo.  When I made this change, I was
glad to see the first element in the test case output was "<gork>".
Unfortunately, when I looked at the data in the element, I could see it
should have been "<bar>".  I looked at the rest of the results, and all
of them were "<gork>", whether they should be or not.

> -----Original Message-----
> From: KARR, DAVID (ATTCINW) [mailto:dk068x@att.com]
> Sent: 19 December 2009 19:50
> To: users@cxf.apache.org
> Subject: serializing subclass of base class gets base class name in
> output
> 
> I had originally had a "Foo" class in my REST server which was
> serializing fine as "<foo>".  I then realized that this was really a
> base class, so I added the subclasses of it, which are "Bar" and
> "Gork".
> The declaration of the list of these is the following:
> 
>     @XmlElementWrapper(name = "childFoos")
>     @XmlElement(name = "foo")
>     private List<Foo>   childFoos;
> 
> When I serialize this, I get exactly what I specified here, such that
> each "Foo" element is a "foo".
> 
> What I want is to have a "<childFoos>" list, but each element will
> either be "<bar>" or "<gork>", depending on the subclass.  The data I
> have in each "foo" is correct, reflecting whether it was a "bar" or a
> "gork", but I want the wrapper to change based on that.
> 
> I tried to remove the "XmlElement" wrapper here, but that just
resulted
> in "<childFoos>" wrapping a list of "<childFoos>", not a mixture of
> "<bar>" or "<gork>" elements.

RE: serializing subclass of base class gets base class name in output

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

You may want to try @XmlElements listing Bar.class and Gork.class
instead of @XmlElement(name = "foo"), let us know please if it helps

Cheers, Sergey.

-----Original Message-----
From: KARR, DAVID (ATTCINW) [mailto:dk068x@att.com] 
Sent: 19 December 2009 19:50
To: users@cxf.apache.org
Subject: serializing subclass of base class gets base class name in
output

I had originally had a "Foo" class in my REST server which was
serializing fine as "<foo>".  I then realized that this was really a
base class, so I added the subclasses of it, which are "Bar" and "Gork".
The declaration of the list of these is the following:

    @XmlElementWrapper(name = "childFoos")
    @XmlElement(name = "foo")
    private List<Foo>   childFoos;

When I serialize this, I get exactly what I specified here, such that
each "Foo" element is a "foo".

What I want is to have a "<childFoos>" list, but each element will
either be "<bar>" or "<gork>", depending on the subclass.  The data I
have in each "foo" is correct, reflecting whether it was a "bar" or a
"gork", but I want the wrapper to change based on that.

I tried to remove the "XmlElement" wrapper here, but that just resulted
in "<childFoos>" wrapping a list of "<childFoos>", not a mixture of
"<bar>" or "<gork>" elements.