强制Spring RestTemplate将纯文本处理为JSON? [英] Force spring RestTemplate to process plain text as JSON?
问题描述
Web API正在使用Content-Type:text/plain; charset=utf-8
响应请求,但是消息的格式像是JSON,例如
A web API is responding to a request with Content-Type:text/plain; charset=utf-8
, but the messageis formatted as if it were a JSON, eg.
{
"total": 168,
"page": 0,
"pageCount": 1,
...
}
在Spring中,此消息使用RestTemplate处理,并且JSON自动映射到ModelDto
POJO,
In Spring, this message is processed with a RestTemplate and the JSON is automagically mapped into a ModelDto
POJO,
restTemplate.getForObject(url, ModelDto::class.java)
这会出现以下错误:
org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[class api.ModelDto]和内容类型[text/plain; charset = utf-8]找到合适的HttpMessageConverter
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class api.ModelDto] and content type [text/plain;charset=utf-8]
尽管Content-Type是纯文本的,有没有办法让spring将此消息视为JSON并解析为JSON?
Is there any way to have spring treat this message as if it were a JSON and parse it as such, despite the Content-Type being plaintext?
推荐答案
更新
因为AbstractHttpMessageConverter
具有方法setSupportedMediaTypes
可用于更改支持的媒体类型,所以无需创建自定义HttpMessageConverter
:
There's no need to create a custom HttpMessageConverter
since AbstractHttpMessageConverter
has a method setSupportedMediaTypes
which can be used to change supported media type:
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON));
restTemplate.getMessageConverters().add(0, converter);
我认为可以通过实现自己的HttpMessageConverter<T>
来实现.
I think it's possible via implementing own HttpMessageConverter<T>
.
RestTemplate
使用它将原始响应转换为某种表示形式(例如POJO).由于它具有转换器列表,因此可以根据其类型(例如application/json
等)找到用于特定响应的特定转换器.
RestTemplate
uses it to convert a raw response to some representation (for instance, POJO). Since it has a list of converters, it finds specific converter for a particular response by its type (e.g application/json
, etc).
因此,HttpMessageConverter<T>
的实现应类似于默认的MappingJackson2HttpMessageConverter
,但支持的媒体类型已更改:
So your implementation of HttpMessageConverter<T>
should be something like default MappingJackson2HttpMessageConverter
but with changed supported media type:
public class MappingJackson2HttpMessageConverter2 extends AbstractJackson2HttpMessageConverter {
private String jsonPrefix;
public MappingJackson2HttpMessageConverter2() {
this(Jackson2ObjectMapperBuilder.json().build());
}
public MappingJackson2HttpMessageConverter2(ObjectMapper objectMapper) {
// here changed media type
super(objectMapper, MediaType.TEXT_PLAIN);
}
public void setJsonPrefix(String jsonPrefix) {
this.jsonPrefix = jsonPrefix;
}
public void setPrefixJson(boolean prefixJson) {
this.jsonPrefix = (prefixJson ? ")]}', " : null);
}
@Override
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
if (this.jsonPrefix != null) {
generator.writeRaw(this.jsonPrefix);
}
String jsonpFunction =
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
if (jsonpFunction != null) {
generator.writeRaw("/**/");
generator.writeRaw(jsonpFunction + "(");
}
}
@Override
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
String jsonpFunction =
(object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null);
if (jsonpFunction != null) {
generator.writeRaw(");");
}
}
}
然后您可以将其添加到RestTemplate
对象:
Then you can add this to RestTemplate
object:
restTemplate.getMessageConverters().add(0, new MappingJackson2HttpMessageConverter2());
这篇关于强制Spring RestTemplate将纯文本处理为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!