jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

@JacksonXmlElementWrapper Conflicting getter/setter definitions for property

Open n00bman opened this issue 12 years ago • 10 comments

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: <beanInfo>name</beanInfo><beanOther>name</beanOther>

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...

n00bman avatar Apr 26 '12 14:04 n00bman

Yes, I can reproduce this issue, added a unit test. Hope to resolve it.

cowtowncoder avatar May 24 '12 05:05 cowtowncoder

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.

cowtowncoder avatar Sep 24 '12 02:09 cowtowncoder

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 avatar Oct 09 '17 18:10 madrob

@madrob Unlikely this would get resolved any time soon.

cowtowncoder avatar Oct 10 '17 20:10 cowtowncoder

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 avatar Nov 30 '17 14:11 mikaelhg

@mikaelhg did you find a solution for this that didn't involve wrapping the objects?

RikkiGibson avatar Jan 26 '18 19:01 RikkiGibson

@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.

mikaelhg avatar Jan 26 '18 21:01 mikaelhg

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.

RikkiGibson avatar Jan 26 '18 21:01 RikkiGibson

how to solve this problem?

zhangqunshi avatar Jun 11 '20 09:06 zhangqunshi