有时我会收到一个设置了content-type=application/octet-stream
的Web服务响应,无论如何实际上是application/xml
。
由于我无法控制Web服务,所以我仍然想告诉杰克逊(我正在与spring-boot
一起使用)将这些响应解析为xml。但是如何?
我首先尝试让杰克逊映射器完全允许八位字节流,到目前为止,它仍然有效:
@Bean
public RestTemplateCustomizer customizeJackson2MessageConverter() {
return restTemplate -> {
for (var converter : restTemplate.getMessageConverters()) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
jackson.getSupportedMediaTypes().add((MediaType.APPLICATION_OCTET_STREAM);
}
}
};
}
但是:当涉及到解析时,如何告诉杰克逊忽略/重写内容类型,仍然将其解析为普通的json?
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: (ByteArrayInputStream); line: 1, column: 2]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102) ~[spring-web-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 113 more
答案 0 :(得分:0)
我按照@chrylis的建议进行了操作
DataType