XML 转换失败 Spring Boot RestTemplate
XML conversion fails with Spring Boot RestTemplate
鉴于这个非常基本的 Spring 引导应用程序:
@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
:
的 object
ResponseEntity<IceCatResponse> result = this.httpTemplate.exchange(url, HttpMethod.GET, request, IceCatResponse.class);
with request
仅包含身份验证 Header。
这会导致错误消息
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.]
我认为响应的 DOCTYPE 部分是我的问题,有谁知道,我如何配置 Spring 忽略这部分?
找到一个脏的解决方案:
@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;
}
现在,我的自定义对象的解析已按预期完成,但感觉这个解决方案不像 "Spring-way" 那样...
鉴于这个非常基本的 Spring 引导应用程序:
@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);
with request
仅包含身份验证 Header。
这会导致错误消息
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.]
我认为响应的 DOCTYPE 部分是我的问题,有谁知道,我如何配置 Spring 忽略这部分?
找到一个脏的解决方案:
@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;
}
现在,我的自定义对象的解析已按预期完成,但感觉这个解决方案不像 "Spring-way" 那样...