我正在尝试使用 Spring 的 RestTemplate 将支付提供商实施到我正在从事的项目中。从支付提供商返回的 XML 如下:
<?xml version="1.0" ?>
<response>
<bank>
<bank_id>0031</bank_id>
<bank_name>ABN AMRO</bank_name>
</bank>
<bank>
<bank_id>0761</bank_id>
<bank_name>ASN Bank</bank_name>
</bank>
<bank>
<bank_id>0091</bank_id>
<bank_name>Friesland Bank</bank_name>
</bank>
<bank>
<bank_id>0721</bank_id>
<bank_name>ING</bank_name>
</bank>
<bank>
<bank_id>0021</bank_id>
<bank_name>Rabobank</bank_name>
</bank>
<bank>
<bank_id>0771</bank_id>
<bank_name>RegioBank</bank_name>
</bank>
<bank>
<bank_id>0751</bank_id>
<bank_name>SNS Bank</bank_name>
</bank>
<bank>
<bank_id>0511</bank_id>
<bank_name>Triodos Bank</bank_name>
</bank>
<bank>
<bank_id>0161</bank_id>
<bank_name>van Lanschot</bank_name>
</bank>
<message>This is the current list of banks and their ID's that currently support iDEAL-payments</message>
</response>
我为此 XML 创建的类是:
@XmlRootElement(name="response")
public class ResponseBanks {
private List<Bank> banks;
private String message;
public void setBanks(List<Bank> banks) {
this.banks = banks;
}
@XmlElement(name="bank")
public List<Bank> getBanks() {
return banks;
}
@XmlElement(name="message")
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
@XmlRootElement(name="bank")
public class Bank {
private String bank_id;
private String bank_name;
@XmlElement(name="bank_id")
public String getBank_id() {
return bank_id;
}
public void setBank_id(String bank_id) {
this.bank_id = bank_id;
}
@XmlElement(name="bank_name")
public String getBank_name() {
return bank_name;
}
public void setBank_name(String bank_name) {
this.bank_name = bank_name;
}
}
如果我只是将 xml 请求为字符串并自己解组它们,它可以工作:
String banksAsString = restTemplate.getForObject("https://secure.mollie.nl/xml/ideal?a=banklist", String.class);
try {
JAXBContext jc = JAXBContext.newInstance(ResponseBanks.class);
Unmarshaller um = jc.createUnmarshaller();
ResponseBanks banks = (ResponseBanks) um.unmarshal(new StringReader(banksAsString));
}
catch (JAXBException e) {
e.printStackTrace();
}
但是,如果我这样做:
ResponseBanks banksAsObject = restTemplate.getForObject("https://secure.mollie.nl/xml/ideal?a=banklist", ResponseBanks.class);
或者
Source banksAsSource = restTemplate.getForObject("https://secure.mollie.nl/xml/ideal?a=banklist", Source.class);
它以 406 Not Acceptable 告终。
我的restTemplate beanconfiguration(在控制器中是@Autowired)如下所示:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller"/>
<property name="unmarshaller" ref="jaxbMarshaller"/>
</bean>
</list>
</property>
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>nl.mollie.api.ResponseBanks</value>
</list>
</property>
</bean>
有人知道是什么原因造成的以及如何解决吗?上面代码中的 URL 是可公开访问的,因此您可以自己尝试此代码。