杰克逊XML 2.9.0:@JacksonXmlElementWrapper无法与@JsonCreator和@JsonProperty构造函数一起使用

时间:2019-12-01 10:28:33

标签: java xml jackson xml-deserialization jackson-dataformat-xml

我希望我的ParentClassfinal个字段,'brokenChildList'列表包装了xml元素,并且列表项的标签与列表(<brokenChildList><brokenChild/></brokenChildList>)不同。

这是一段代码来重现我的问题(导入被部分截断,省略了setter和getter)

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class Main {
    public static void main(String... args) throws IOException {
        ObjectMapper xmlMapper = new XmlMapper();
        String xmlString = "<ParentClass><childClass name=\"name1\" value=\"val1\"/><brokenChildList><brokenChild name=\"bc1\" reason=\"bc-val1\"/><brokenChild name=\"bc2\" reason=\"bc-val2\"/></brokenChildList></ParentClass>";
        ParentClass parentClass = xmlMapper.readValue(xmlString, ParentClass.class);
        StringWriter stringWriter = new StringWriter();
        xmlMapper.writeValue(stringWriter, parentClass);
        String serialised = stringWriter.toString();
        System.out.println(serialised);
        System.out.println(xmlString.equals(serialised));
    }

    public static class ChildClass {
        @JacksonXmlProperty(isAttribute = true)
        private String name;
        @JacksonXmlProperty(isAttribute = true)
        private String value;
        //getters & setters
    }
    public static class BrokenChild {
        @JacksonXmlProperty(isAttribute = true)
        private String name;
        @JacksonXmlProperty(isAttribute = true)
        private String reason;
        //getters & setters
    }
    public static class ParentClass {
        private final ChildClass childClass;
        private final List<BrokenChild> brokenChildList;
        @JsonCreator
        public ParentClass(
            @JsonProperty("childClass") ChildClass childClass,
            @JsonProperty("brokenChildList") List<BrokenChild> brokenChildList
        ) {
            this.childClass = childClass;
            this.brokenChildList = brokenChildList;
        }

        @JacksonXmlProperty(localName = "childClass")
        public ChildClass getChildClass() {
            return childClass;
        }

        @JacksonXmlElementWrapper(localName = "brokenChildList")
        @JacksonXmlProperty(localName = "brokenChild")
        public List<BrokenChild> getBrokenChildList() {
            return brokenChildList;
        }
    }
}

以上代码提供了Jackson版本2.8.10的输出:

<ParentClass><childClass name="name1" value="val1"/><brokenChildList><brokenChild name="bc1" reason="bc-val1"/><brokenChild name="bc2" reason="bc-val2"/></brokenChildList></ParentClass>
true

对于Jackson版本2.9.0,它给出:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Duplicate property 'brokenChildList' for [simple type, class org.test.Main$ParentClass]
 at [Source: (StringReader); line: 1, column: 1]

我想找到一个解决方案(以及2.9.0之后的任何版本),该解决方案将与附带的代码给出相同的输出。

我失败的尝试包括:

  • @JacksonXmlElementWrapper(localName = "brokenChildList")替换@JacksonXmlElementWrapper会将包装器元素重命名为'brokenChild',这是不希望的。

  • 删除@JacksonXmlElementWrapper(localName = "brokenChildList")会将包装器元素重命名为'brokenChild',这是不希望的。

1 个答案:

答案 0 :(得分:0)

这个问题确实棘手,因为Jackson从不同的地方收集元数据:字段,getter,setter,构造函数参数。另外,您可以使用MixIn,但不会出现这种情况。

可以将

@JacksonXmlElementWrapper批注附加到FIELDMETHOD类型的元素上,这迫使您在getter上声明它。因为ParentClass是不可变的,并且您想使用构造函数构建它,所以我们还需要注释构造函数参数。这就是发生冲突的地方:您有一个带有@JsonProperty("brokenChildList")批注的构造函数参数和一个带有@JacksonXmlElementWrapper(localName = "brokenChildList")的getter,该参数重用了相同的名称。如果将localName更改为@JacksonXmlElementWrapper(localName = "brokenChildListXYZ")(添加XYZ),则所有内容都会被反序列化和序列化,但是输出将不同于输入。

要解决此问题,我们可以使用com.fasterxml.jackson.databind.deser.BeanDeserializerModifier类,该类允许过滤出我们不想用于反序列化的字段,并且会产生冲突。用法示例:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.stream.Collectors;

public class XmlMapperApp {

    public static void main(String... args) throws IOException {
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) {
                if (beanDesc.getBeanClass() == ParentClass.class) {
                    return propDefs.stream().filter(p -> p.getConstructorParameter() != null).collect(Collectors.toList());
                }
                return super.updateProperties(config, beanDesc, propDefs);
            }
        });
        XmlMapper xmlMapper = XmlMapper.xmlBuilder()
                .addModule(module)
                .build();

        //yours code
    }
}

要创建此示例,我使用了2.10.0版。

另请参阅: