aay*_*hkr 6 java pdf spring resttemplate
我正在尝试捕获我的java客户端代码发出的HTTP请求的响应.响应的内容类型为application/pdf.在日志中我可以看到服务器发送了响应
Object result = getRestTemplate().postForObject(urlString, formDataHttpEntity, returnClassObject, parametersMapStringString);
Run Code Online (Sandbox Code Playgroud)
我得到以下JUnit错误:
org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[java.lang.Object]和内容类型[application/pdf]找到合适的HttpMessageConverter
我需要做些什么来克服这个问题?我的最终目标是byte[]将其添加到一个blob类型的数据库表字段中
注意:我从服务器获得以下响应头
HTTP/1.1 200 OK Cache-Control:max-age = 0,必须重新验证
Content-Disposition:attachment; filename ="Executive Summary.PDF"
Content-Type:application/pdf
aay*_*hkr 12
谢谢托马斯的工作.
我将ByteArrayHttpMessageConverter添加到RestTemplate并且它有效.
我添加的代码:
ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
List<MediaType> supportedApplicationTypes = new ArrayList<MediaType>();
MediaType pdfApplication = new MediaType("application","pdf");
supportedApplicationTypes.add(pdfApplication);
byteArrayHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(byteArrayHttpMessageConverter);
restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
Object result = getRestTemplate().getForObject(url, returnClass, parameters);
byte[] resultByteArr = (byte[])result;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11908 次 |
| 最近记录: |