java - 如何使用 RestTemplate 从服务器接收应用程序/pdf 响应

标签 java spring pdf resttemplate

我正在 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/

相关文章:

Java获取以色列当前时间

java - 如何使用jSTL获取jsp中日历类型方法中设置的日期?

java - 如何获取要在 Spring 中创建的 bean 集?

Spring Cloud zuul "path": "/actuator/routes" 404 not found

java - 如何在 Struts 中为 Mysql 的搜索结果创建 pdf

java - 如何识别 PDF 文件是否包含表单域

Java String.使用 OR 管道匹配正则表达式

java - 当我们从开始菜单中单击隐藏页脚和页眉的应用程序时打开浏览器

Spring OAuth授权服务器和资源服务器位于不同的服务器上

java - BIRT 3.7 运行时。不能再自定义 PDF 字体和 jdbc 驱动程序了吗?