我正在使用RestTemplate调用一个rest服务,它返回一个很大的csv文件.我使用了下面的代码.我正在try 将响应存储在文件中.
导入java.nio.file.Files; 导入java.nio.file.Path;
String fileName = "response.csv";
File file = new File(fileName);
Path filePath = file.toPath();
HttpEntity<Object> requestEntity = new HttpEntity<>(null, headers);
RestTemplate restTemplate = new RestTemplate();
InputStream responseStream = restTemplate
.exchange(serviceURL/LogFile"), HttpMethod.GET, requestEntity, InputStream.class).getBody();
try {
Files.copy(responseStream, filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我得到了这个例外.
RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.InputStream] and content type [text/csv]
根据我的理解,我们需要编写合适的消息转换器和添加/注册将REST模板.
How to write suitable converter to get the csv response and store in a file?个