Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive application/pdf response from a server using RestTemplate

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);

and I get the following JUnit error:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [java.lang.Object] and content type [application/pdf]

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 Cache-Control: max-age=0,must-revalidate
Content-Disposition: attachment; filename="Executive Summary.PDF"
Content-Type: application/pdf

like image 381
ak1984 Avatar asked Aug 26 '14 16:08

ak1984


1 Answers

Thanks Thomas it worked.

I added ByteArrayHttpMessageConverter to the RestTemplate and it worked.

Code I added:

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;
like image 114
ak1984 Avatar answered Oct 16 '22 23:10

ak1984