This repository was archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
XmlElementRef ignored if inside XmlElementWrapper #40
Copy link
Copy link
Closed
Milestone
Description
It seams that JaxbAnnotationIntrospector
is ignoring @XmlElementRef
annotations if they are coupled with @XmlElementWrapper
. For example, take this two simple beans:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
@XmlElement
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
@XmlElementWrapper(name="data")
@XmlElementRef
public List<Bar> getBars() { return bars; }
public void setBars(List<Bar> bars) { this.bars = bars; }
private int count;
private List<Bar> bars;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Bar {
@XmlElement
public String getFirst() { return first; }
public void setFirst(String first) { this.first = first; }
@XmlElement
public String getSecond() { return second; }
public void setSecond(String second) { this.second = second; }
private String first;
private String second;
}
and this data:
Foo foo = new Foo();
foo.setCount(7);
List<Bar> bars = new ArrayList<Bar>();
Bar one = new Bar();
one.setFirst("One");
one.setSecond("Only");
bars.add(one);
Bar two = new Bar();
two.setFirst("Two");
two.setSecond("Trees");
bars.add(two);
foo.setBars(bars);
When serialized to XML (using JAXB.marshal(...)
) output is fine:
<foo>
<data>
<bar>
<first>One</first>
<second>Only</second>
</bar>
<bar>
<first>Two</first>
<second>Trees</second>
</bar>
</data>
<count>7</count>
</foo>
However, when serializing to JSON (using ObjectMapper.writeValue(...)
) whole
"data" part is missing and output is just:
{
"count" : 7
}
I've found out, that I can get correct JSON output if I use @XmlElement
instead of @XmlElementRef
on Foo.bars
:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
@XmlElement
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
@XmlElementWrapper(name="data")
@XmlElement
public List<Bar> getBars() { return bars; }
public void setBars(List<Bar> bars) { this.bars = bars; }
private int count;
private List<Bar> bars;
}
Output:
{
"count" : 7,
"data" : [ {
"first" : "One",
"second" : "Only"
}, {
"first" : "Two",
"second" : "Trees"
} ]
}
Unfortunatelly, changing @XmlElementRef
to @XmlElement
is not viable
solution in complex project.
Note: in all above examples MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME
was set to true.
Metadata
Metadata
Assignees
Labels
No labels