【问题标题】:XML conversion fails with Spring Boot RestTemplate使用 Spring Boot RestTemplate 进行 XML 转换失败
【发布时间】:2017-06-23 06:18:33
【问题描述】:

鉴于这个非常基本的 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 */
}

卷曲所需的 URL 产生

<?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: 无法解组为 [class de.mischok.konfigurator.spikeicecat.model.IceCatResponse]:空; 嵌套异常是 javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException;行号:2;列号:10; DOCTYPE ist nicht zulässig, wenn das Feature "http://apache.org/xml/features/disallow-doctype-decl" auf "真" gsetzt ist.]

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

【问题讨论】:

    标签: xml spring spring-boot resttemplate


    【解决方案1】:

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

    @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;
    }
    

    现在对我的自定义对象的解析已按预期完成,但这个解决方案感觉不像是“弹簧方式”......

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2016-07-07
      • 2015-09-07
      • 2021-03-17
      • 1970-01-01
      • 2019-05-29
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多