问题很简单: 我如何将这样的xml文档解析为POJO?
XML
<ArrayOfiDealIssuerCountry xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Aspider.iDeal.Domain">
<iDealIssuerCountry>
<CountryNames>CountryName</CountryNames>
<Issuers>
<iDealIssuer>
<ID>1</ID>
<Name>Bank A</Name>
</iDealIssuer>
<iDealIssuer>
<ID>2</ID>
<Name>Bank B</Name>
</iDealIssuer>
</Issuers>
</iDealIssuerCountry>
</ArrayOfiDealIssuerCountry>
我现在拥有的是
请求:
ArrayList<Issuers> issuers = restTemplate.exchange(builder.build().encode().toString(),HttpMethod.GET,
entity, new ParameterizedTypeReference<ArrayList<Issuers>>(){}).getBody();
性反应:
[Issuers [countryName=null, issuers=[]]]
Issuers.java:
@XmlRootElement(name = "iDealIssuerCountry")
public class Issuers {
@XmlElement(name = "CountryNames", required = true)
String countryNames;
@XmlElement(name = "Issuers", required = true)
ArrayList<Issuer> issuers = new ArrayList<Issuer>();
/// getters and setters
}
Issuer.java:
@XmlRootElement(name = "iDealIssuer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"ID", "Name"})
public class Issuer {
@XmlElement(name = "ID", required = true)
Long id;
@XmlElement(name = "Name", required = true)
String name;
// getters and setters
}
响应机构是下一个:
[
{
"CountryNames":"CountryName",
"Issuers":
[
{"ID":1,"Name":"Bank A"},
{"ID":2,"Name":"Bank B"}
]
}
]
我认为我在xml doc到java类的映射中遗漏了一些东西。 谁能指出我的错误?
答案 0 :(得分:0)
查看JAXB。有关更多信息,请访问:
http://www.oracle.com/technetwork/articles/javase/index-140168.html
答案 1 :(得分:0)
解决:
我已经注释了我的POJO构造函数,如下所示:
@JsonCreator
public IssuersCountry(@JsonProperty("CountryNames") String CountryNames,@JsonProperty("Issuers") ArrayList<Issuer> issuers) {...}
用于反序列化:
List<IssuersCountry> issuers = new ObjectMapper().readValue(responseBody, new TypeReference<List<IssuersCountry>>(){});