Spring启动休息模板-HttpMessageConverter无法转换对象



大家好,我有一个问题,我不明白为什么会发生这种情况。我正在使用spring-boot并使用Kubernetes进行部署。过了一段时间,restTemplate停止工作,我不知道原因是什么。这就是我创建restTemplate类的方式

@Bean
public RestTemplate vanillaRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestTemplateErrorHandler());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
return restTemplate;
}

过了一段时间,我的一些使用restTemplate的API返回如下错误:

{
"status": 500,
"body": "{ message: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [application/json;charset=UTF-8] , cause: null }",
}

这只是一个例子。当这种情况发生时,resttemplate将对许多类(如Map、String等(产生问题。这就是我发送请求的方式:

ResponseEntity<List> adsRes = restTemplate.getForEntity(url, List.class);

奇怪的是,当我在服务器上重新启动应用程序时,一切都重新开始工作,API根本没有问题。甚至是那些返回错误的。我读过这个问题,但正如你所看到的,我没有将任何拦截器或类似的东西设置为resttemplate提前感谢

尝试用以下代码替换对端点的调用方式

ResponseEntity<YourObject[]> response = restTemplate.getForEntity("urlService",
YourObject[].class);
YourObject[] yourObjects= response.getBody();
List<YourObject> list = Arrays.asList(yourObjects);