gpt4 book ai didi

java - Spring 休息模板。如何解析服务器对 POJO 的 XML 响应

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:26 25 4
gpt4 key购买 nike

问题很简单: 我怎样才能将这样的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
}

发行者.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 文档到 java 类的映射中遗漏了一些东西。谁能指出我的错误吗?

最佳答案

查看 JAXB。更多信息请参见此处:

http://www.oracle.com/technetwork/articles/javase/index-140168.html

关于java - Spring 休息模板。如何解析服务器对 POJO 的 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com