Nim*_*sky 39 java xml spring jaxb
使用spring,使用以下代码:
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
for(HttpMessageConverter httpMessageConverter : messageConverters){
System.out.println(httpMessageConverter);
}
ResponseEntity<ProductList> productList = restTemplate.getForEntity(productDataUrl,ProductList.class);
Run Code Online (Sandbox Code Playgroud)
我明白了
org.springframework.http.converter.ByteArrayHttpMessageConverter@34649ee4
org.springframework.http.converter.StringHttpMessageConverter@39fba59b
org.springframework.http.converter.ResourceHttpMessageConverter@383580da
org.springframework.http.converter.xml.SourceHttpMessageConverter@409e850a
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@673074aa
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e3b79d3
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@52bb1b26
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycopmany.ProductList] and content type [text/html;charset=UTF-8]
Run Code Online (Sandbox Code Playgroud)
pojo的片段:
@XmlRootElement(name="TheProductList")
public class ProductList {
@XmlElement(required = true, name = "date")
private LocalDate importDate;
Run Code Online (Sandbox Code Playgroud)
Sot*_*lis 38
从Spring的角度来看,没有HttpMessageConverter
注册的实例RestTemplate
可以将text/html
内容转换为ProductList
对象.感兴趣的方法是HttpMessageConverter#canRead(Class, MediaType)
.所有上述回报的实施false
,包括Jaxb2RootElementHttpMessageConverter
.
由于没有HttpMessageConverter
可以读取您的HTTP响应,因此处理失败并出现异常.
如果你能控制服务器响应,修改设置Content-type
到application/xml
,text/xml
或东西匹配application/*+xml
.
如果您不控制服务器响应,则需要编写并注册您自己的HttpMessageConverter
(可以扩展Spring类,请参阅AbstractXmlHttpMessageConverter
及其子类),它们可以读取和转换text/html
.
Mar*_*orn 19
您也可以简单地告诉您RestTemplate
接受所有媒体类型:
@Bean
public RestTemplate restTemplate() {
final RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
Run Code Online (Sandbox Code Playgroud)
如果无法更改服务器媒体类型响应,则可以扩展GsonHttpMessageConverter以处理其他支持类型
public class MyGsonHttpMessageConverter extends GsonHttpMessageConverter {
public MyGsonHttpMessageConverter() {
List<MediaType> types = Arrays.asList(
new MediaType("text", "html", DEFAULT_CHARSET),
new MediaType("application", "json", DEFAULT_CHARSET),
new MediaType("application", "*+json", DEFAULT_CHARSET)
);
super.setSupportedMediaTypes(types);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Spring Boot,则可能需要确保在类路径中具有Jackson依赖项.您可以通过以下方式手动完成:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
或者您可以使用Web启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
除了所有答案外,如果您碰巧希望收到其他答复text/html
(例如application/json
),则可能表明服务器端发生了错误(例如404),并且返回了错误页面而不是数据。
所以就我而言。希望它可以节省别人的时间。
归档时间: |
|
查看次数: |
120732 次 |
最近记录: |