使用restTemplate发出请求后反序列化text / plain

时间:2018-03-10 08:20:46

标签: spring rest deserialization resttemplate

我试图将我的应用程序与ebay Finding API集成,在熟悉api之后,我尝试了一些请求,看看它是否按预期工作,但有一件事让我陷入困境。 ebay服务即使我明确地将RESPONSE-DATA-FORMAT设置为JSON,也以json格式返回响应,但内容类型是text / plain。我将restTemplate消息转换器设置如下:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper)
{
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    messageConverter.setObjectMapper(objectMapper);
    messageConverter.setSupportedMediaTypes(
        ImmutableList
            .of(
                new MediaType("application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET),
                new MediaType("text", "plain", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)
               ));

    return messageConverter;
}

@Bean
RestTemplate restTemplate(MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter)
{
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(Collections.singletonList(mappingJackson2HttpMessageConverter));
    return new RestTemplate();
}

即使我尝试反序列化响应,如下所示: json data

和对象:

public class Response
{
   private String version;

   public Response()
   {
   }

   public String getVersion()
   {
      return version;
   }

   public void setVersion(String version)
   {
      this.version = version;
   }
}

我的Api电话:

String url = uriBuilder.formEndpoint("iphone").toString();
Response response = restTemplate.getForObject(url, Response.class);

像这样的例外结束:

Could not extract response: no suitable HttpMessageConverter found for 
response type [class com.domain.Response] 
and content type [text/plain;charset=UTF-8]

0 个答案:

没有答案