我希望我的ParentClass
具有final
字段,“ 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',这是不可取的。 最佳答案
这个问题确实棘手,因为Jackson
从不同位置收集元数据:字段,getter,setter,构造函数参数。另外,您可以使用MixIn
,但不会出现这种情况。
可以将@JacksonXmlElementWrapper
批注附加到FIELD
和METHOD
类型元素上,这将迫使您在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
版本。也可以看看:
Jackson 2.10 features
Jackson Release 2.10