我希望我的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;         }     } } 

上面的代码给出了杰克逊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 

对于杰克逊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',这是不希望的。

未解决问题?本站智能推荐: