我有一个XML文件作为Springboot REST服务的输入,该XML在结构上看起来像这样:
<someelement id="foo">
<property name="somekey" value="somevalue"></property>
<property name="somekey2" value="somevalue2"></property>
<property name="somekey3" value="somevalue3"></property>
<someelement id="blah">
</someelement>
</someelement>
在告诉杰克逊我的清单没有被包装后,我可以用以下的类来解析它:
@XmlRootElement(name = "someelement")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class SomeElement {
@JacksonXmlProperty(isAttribute = true, localName = "id")
private String id;
@JacksonXmlProperty(localName = "property")
private List<Property> properties;
@JacksonXmlProperty(localName = "someelement")
private List<SomeElement> children;
}
@XmlRootElement(name = "Property")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Property {
@JacksonXmlProperty(isAttribute = true, localName = "name")
private String name;
@JacksonXmlProperty(isAttribute = true, localName = "value")
private String value;
}
@Data
仅生成所有的setter / getter。所以很明显我得到了一个属性列表,这并不是我想要的。我会评价一个Map<String, String>
,其中会解析属性名称和值。
@XmlRootElement(name = "someelement")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class SomeElement {
@JacksonXmlProperty(isAttribute = true, localName = "id")
private String id;
// some annotation here that takes all child property tags
// and parses the name/value pairs into this map
private Map<String, String> properties;
@JacksonXmlProperty(localName = "someelement")
private List<SomeElement> children;
}
我该如何解决类似问题?我是否需要为此编写自己的解串器?只是感觉到我可能缺少一种简单的方法,因为在纸面上这听起来并不那么复杂/罕见。