1

我正在尝试使用 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 是可公开访问的,因此您可以自己尝试此代码。

4

3 回答 3

0

406 Not Acceptable看起来像 MIME 类型/内容协商的问题。

您尝试与之通信的 Web 服务未发送正确的Content-type. 如果此 Web 服务由您或您的同事实现,@Produces("application/xml")您的 JAX-RS Web 服务方法上是否有注释?

更多信息:https ://cwiki.apache.org/WINK/jax-rs-request-and-response-entities.html

于 2012-11-20T00:16:38.347 回答
0

添加作为答案,因为没有足够的声誉得分来发表评论。

@Maciej,您在上面发布的链接无效。您在其他任何地方都有此内容吗?请在发布链接时尝试从链接中放入关键内容。

于 2013-12-05T15:22:06.517 回答
0

要使用Content-typeas 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>
于 2021-03-15T19:20:08.967 回答