所以我有两堂课:
public class Catalog{
private List<Country> countries = new ArrayList<>();
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
}
第二个:
public class Country{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我想要的是 ?
创建 xml 但使用不同的名称
<Catalogue>
<Countries>
<Country>
<name>RO</name>
</Country>
<Country>
<name>RO</name>
</Country>
</Countries>
</Catalogue>
我使用以下方式编写 xml:
Catalogue catalogue = new Catalogue();
Country country = new Country();
country.setName("RO");
catalogue.getCountries().add(country);
catalogue.getCountries().add(country);
String xml = xmlMapper.writeValueAsString(catalogue); // serializing
System.out.println("The xml is " + xml);
我该怎么做呢?
我尝试在元素上使用 @JsonProperty、@JsonGetter 和 @JsonSetter,但我无法执行此操作。
当我添加这些注释时,它会执行类似的操作
<Countries>
<Countries>
<name>RO</name>
</Countries>
</Countries>
JacksonXmlProperty
and JacksonXmlElementWrapper
注释应该适合你:
@JacksonXmlRootElement(localName = "Catalogue")
class Catalog{
@JacksonXmlProperty(localName = "Country")
@JacksonXmlElementWrapper(localName = "Countries")
private List<Country> countries = new ArrayList<>();
//...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)