我正在尝试使用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不可接受。
我对restTemplate的bean配置(在控制器中是@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可以公开访问,因此您可以自己尝试使用此代码。
答案 0 :(得分:0)
406 Not Acceptable
看起来像MIME类型/内容协商有问题。
您尝试与之通信的Web服务未发送正确的Content-type
。
如果您或您的同事实施此Web服务,您的JAX-RS Web服务方法是否有@Produces("application/xml")
注释?
更多信息:https://cwiki.apache.org/WINK/jax-rs-request-and-response-entities.html
答案 1 :(得分:0)
添加作为答案,因为没有足够的声誉评分来评论。
@Maciej,您在上面发布的链接无效。你有其他可用的内容吗? 请在发布链接时尝试从链接中输入关键内容。
答案 2 :(得分:0)
要使用 Content-type
作为 application/xml
,您需要配置以下消息转换器:
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJacksonHttpMessageConverter(objectMapper()));
}
private MappingJackson2XmlHttpMessageConverter createXmlHttpMessageConverter() {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
builder.indentOutput(true);
return new MappingJackson2XmlHttpMessageConverter(builder.build());
}
并将其添加到您的 pom.xml
文件中:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson-dataformat-xml.version}</version>
</dependency>