必需的xml:
anamnese_allgemein
请注意,两个集合中的项标记相同:“路径”是否包含不同的标记:“路径”和“morePaths”
带注释的bean就像(使用lombok ):
<bean>
<paths>
<path>foo/bar/</path>
<path>x/y</path>
<paths>
<morePaths>
<path>a/b/c</path>
<path>j/k/l</path>
<morePaths>
<bean>
问题: 当映射异常时,表示有两个属性映射到同一个标记( path )
@JacksonXmlRootElement(localName="bean")
@Accessors(prefix="_")
@NoArgsConstructor @AllArgsConstructor
public static class TestBean {
@JacksonXmlElementWrapper(localName="paths") @JacksonXmlProperty(localName="path")
@Getter @Setter private Collection<Path> _pathCol;
@JacksonXmlElementWrapper(localName="otherPaths") @JacksonXmlProperty(localName="path")
@Getter @Setter private Collection<Path> _otherPathCol;
}
例外:
@Test
public void test() throws IOException {
XmlMapper xmlMapper = new XmlMapper();
TestBean myBean = new TestBean(Lists.<Path>newArrayList(Path.from("/a/b/c"),
Path.from("d/e/f")),
Lists.<Path>newArrayList(Path.from("/foo/bar/baz"),
Path.from("x/y/z")));
System.out.println("XML: " + xmlMapper.writeValueAsString(myBean));
}
如果使用不同的属性名称,一切正常:
com.fasterxml.jackson.databind.JsonMappingException: Multiple fields representing property "path"
但是,xml输出不是必需的输出:
@JacksonXmlRootElement(localName="bean")
@Accessors(prefix="_")
@NoArgsConstructor @AllArgsConstructor
public static class TestBean {
@JacksonXmlElementWrapper(localName="paths") @JacksonXmlProperty(localName="paths_path")
@Getter @Setter private Collection<Path> _pathCol;
@JacksonXmlElementWrapper(localName="otherPaths") @JacksonXmlProperty(localName="otherPaths_path")
@Getter @Setter private Collection<Path> _otherPathCol;
}
如何设法在两个集合中获取具有相同标记的项目?
注意: 实际上这是一个已知问题(参见:https://github.com/FasterXML/jackson-dataformat-xml/issues/192) ......但我不知道是否有一个工作场所