Spring Boot RestTemplate的XML转换失败

时间:2017-02-06 10:31:53

标签: xml spring spring-boot resttemplate

鉴于这个非常基本的Spring Boot应用程序:

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

使用Maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

和类型

@XmlRootElement(name = "ICECAT-interface")
public class IceCatResponse {
    private Product product;

    /* getters, setters omitted */
}

@XmlRootElement(name = "Product")
public class Product {

    private int code;

    /* getters, setters omitted */
}

卷曲所需的网址

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ICECAT-interface SYSTEM "http://data.icecat.biz/dtd/ICECAT-interface_response.dtd">
<!-- source: Icecat.biz 2017 -->
<ICECAT-interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://data.icecat.biz/xsd/ICECAT-interface_response.xsd">
    <Product Code="1"
        HighPic="http://images.icecat.biz/img/norm/high/21900316-8020.jpg"
        ...>
        ...
    </Product>
</ICECAT-interface>

我正在尝试通过RestTemplate执行此调用,并希望将结果解析为IceCatResponse类型的对象:

ResponseEntity<IceCatResponse> result = this.httpTemplate.exchange(url, HttpMethod.GET, request, IceCatResponse.class);

request仅包含身份验证标头。

这会导致错误消息

  

无法读取HTTP消息:   org.springframework.http.converter.HttpMessageNotReadableException:   无法解散[班级   de.mischok.konfigurator.spikeicecat.model.IceCatResponse]:null;   嵌套异常是javax.xml.bind.UnmarshalException     - 链接异常:[org.xml.sax.SAXParseException; lineNumber:2; columnNumber:10; DOCTYPE istnichtzulässig,wenn das Feature   “http://apache.org/xml/features/disallow-doctype-decl”auf“true”   gesetzt ist。]

我认为响应的DOCTYPE部分是我的问题,有谁知道,我如何配置Spring来忽略这部分?

1 个答案:

答案 0 :(得分:0)

找到一个肮脏的解决方案:

@Bean
public RestTemplate restTemplate() {
    RestTemplate result = new RestTemplate();

    for (final Iterator<HttpMessageConverter<?>> iterator = result.getMessageConverters().iterator(); iterator.hasNext();) {
        HttpMessageConverter<?> next = iterator.next();
        if (next instanceof Jaxb2RootElementHttpMessageConverter) {
            Jaxb2RootElementHttpMessageConverter jaxbConverter = (Jaxb2RootElementHttpMessageConverter) next;
            jaxbConverter.setSupportDtd(true);
        }
    }

    return result;
}

现在解析到我的自定义对象是按预期完成的,但是这个解决方案并不像#春天&#34;这样做......