Search code examples
xmlspringspring-bootresttemplate

XML conversion fails with Spring Boot RestTemplate


Given this very basic Spring Boot application:

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

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

with Maven dependencies

<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>

And the types

@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 */
}

Curling the required URL yields

<?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>

I'm trying to execute this call via RestTemplate and want to get the result parsed into an object of type IceCatResponse:

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

with request containing just an Authentication Header.

This leads to the error message

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class de.mischok.konfigurator.spikeicecat.model.IceCatResponse]: null; nested exception is javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 10; DOCTYPE ist nicht zulässig, wenn das Feature "http://apache.org/xml/features/disallow-doctype-decl" auf "true" gesetzt ist.]

I think the DOCTYPE part of the response is my problem, does anyone know, how I could configure Spring to ignore this part?


Solution

  • Found a dirty solution:

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

    Now the parsing into my custom object is done as expected, but this solution doesn't feel like the "Spring-way" to do this...