jackson-dataformat-xml
jackson-dataformat-xml copied to clipboard
@JacksonXmlElementWrapper Conflicting getter/setter definitions for property
Hi, I'm using latest release 2.0.1 and I've got a problem with serializing/deserializing xml (everything works fine in JSON). I'd like to have such a xml:
public class WrapperTest {
public static void main(String[] args) throws JsonGenerationException,
JsonMappingException, IOException {
ObjectMapper mapper = new XmlMapper();
Bean bean = new Bean();
BeanInfo beanInfo = new BeanInfo();
beanInfo.setName("name");
BeanInfo beanOther = new BeanInfo();
beanOther.setName("name");
bean.setBeanInfo(new BeanInfo[] { beanInfo });
bean.setBeanOther(new BeanInfo[] { beanOther });
String output = mapper.writeValueAsString(bean);
System.out.println(output);
}
@JacksonXmlRootElement(localName = "output")
private static class Bean {
private BeanInfo[] beanInfo;
private BeanInfo[] beanOther;
@JacksonXmlElementWrapper(localName = "beanInfo")
@JacksonXmlProperty(localName = "item")
public BeanInfo[] getBeanInfo() {
return beanInfo;
}
@JacksonXmlElementWrapper(localName = "beanInfo")
public void setBeanInfo(BeanInfo[] beanInfo) {
this.beanInfo = beanInfo;
}
@JacksonXmlElementWrapper(localName = "beanOther")
@JacksonXmlProperty(localName = "item")
public BeanInfo[] getBeanOther() {
return beanOther;
}
@JacksonXmlElementWrapper(localName = "beanOther")
public void setBeanOther(BeanInfo[] beanOther) {
this.beanOther = beanOther;
}
}
private static class BeanInfo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
This example give getter definition conflict.
If I change
@JacksonXmlElementWrapper(localName = "beanOther")
@JacksonXmlProperty(localName = "item2")
public BeanInfo[] getBeanOther() {
}
then all work fine and I've got
<output><beanInfo><item><name>name</name></item></beanInfo><beanOther><item2><name>name</name></item2></beanOther></output>
but if I try to deserialize it then I've got setter definition conflict...
Yes, I can reproduce this issue, added a unit test. Hope to resolve it.
I now understand what is happening -- property names ("inner" names) are being used for property maps, and result in conflicts, before wrappers would untangle everything. Not sure how to resolve this, but this might be solvable along with the question of whether wrapper should or should not be used for JSON.
This issue has been around for a while, I'm currently upgrading my application from org.codehaus.jackson v1 to com.fasterxml.jackson v2 and hitting this as a regression. Does anybody have a workaround or an ETA for a fix on it?
@madrob Unlikely this would get resolved any time soon.
Bumped into this issue in a project where I'm trying to replace GSON for JSON and xstream for XML with Jackson. Since I can't wrap the objects, I'll try to customize XmlSerializerProvider
to overcome this, either directly or through annotation processing.
@mikaelhg did you find a solution for this that didn't involve wrapping the objects?
@RikkiGibson Yes, but since this was the very last commit I made for my previous employer, I don't have access to the partial implementation which I got semi-working. Customizing collection XML serialization to look exactly like xstream was a giant PITA, and required reading through a lot of both Jackson XML and the relevant core code, and then stepping through it with a debugger. Maybe I'm just stupid, YMMV.
I doubt you are :) But since I have full control over my model classes and don't need superfast serialization performance I'm considering just using JAXB for XML serialization and using Jackson for JSON. Thanks for letting me know.
how to solve this problem?