我正在 try catch 由我的 Java 客户端代码发出的 HTTP 请求的响应。响应的内容类型为 application/pdf
。在日志中,我可以看到服务器在
Object result = getRestTemplate().postForObject(urlString, formDataHttpEntity, returnClassObject, parametersMapStringString);
我收到以下 JUnit 错误:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.Object] and content type [application/pdf]
我需要做什么才能克服这个问题?我的最终目标是将其放入 byte[]
并将其推送到 blob 类型的数据库表字段中
注意:我从服务器得到以下响应头
HTTP/1.1 200 OK Cache-Control: max-age=0,must-revalidate
Content-Disposition: attachment; filename="Executive Summary.PDF"
Content-Type: application/pdf
最佳答案
感谢 Thomas,它成功了。
我将 ByteArrayHttpMessageConverter 添加到 RestTemplate 并且它起作用了。
我添加的代码:
ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
List<MediaType> supportedApplicationTypes = new ArrayList<>();
MediaType pdfApplication = new MediaType("application","pdf");
supportedApplicationTypes.add(pdfApplication);
byteArrayHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(byteArrayHttpMessageConverter);
restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
Object result = getRestTemplate().getForObject(url, returnClass, parameters);
byte[] resultByteArr = (byte[])result;
关于java - 如何使用 RestTemplate 从服务器接收应用程序/pdf 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25510498/