如何使用RestTemplate从服务器接收应用程序/pdf响应 [英] How to receive application/pdf response from a server using RestTemplate
问题描述
我正在尝试捕获由我的Java客户端代码发出的HTTP请求的响应.响应的内容类型为application/pdf
.在日志中,我可以看到服务器在
I am trying capture the response of an HTTP request made by my java client code. The response has a content-type of application/pdf
. In the logs I can see that the server sent a response in
Object result = getRestTemplate().postForObject(urlString, formDataHttpEntity, returnClassObject, parametersMapStringString);
,我收到以下JUnit错误:
and I get the following JUnit error:
org.springframework.web.client.RestClientException:无法提取响应:未为响应类型找到合适的HttpMessageConverter [java.lang.Object]和内容类型[application/pdf]
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类型的DB表字段中
What do I need to do to get past this? My ultimate goal is to take this in a byte[]
and push it in a DB table field of blob type
注意:我从服务器获取以下响应标头
Note: I get the following response header from the server
HTTP/1.1 200 OK缓存控制:max-age = 0,必须重新验证
内容处置:附件; filename =行政摘要.PDF"
内容类型:application/pdf
HTTP/1.1 200 OK Cache-Control: max-age=0,must-revalidate
Content-Disposition: attachment; filename="Executive Summary.PDF"
Content-Type: application/pdf
推荐答案
感谢Thomas的工作.
Thanks Thomas it worked.
我在RestTemplate中添加了ByteArrayHttpMessageConverter,并且可以正常工作.
I added ByteArrayHttpMessageConverter to the RestTemplate and it worked.
我添加的代码:
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;
这篇关于如何使用RestTemplate从服务器接收应用程序/pdf响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!