How to get application / pdf response from server using RestTemplate

I am trying to capture an HTTP request response made by my java client code. The response is of the application/pdf content type. In the logs, I see that the server sent a response to

 Object result = getRestTemplate().postForObject(urlString, formDataHttpEntity, returnClassObject, parametersMapStringString); 

and I get the following JUnit error:

org.springframework.web.client.RestClientException: could not retrieve response: no suitable HttpMessageConverter was 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 accept this in byte[] and push it into the blob database table field 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

+5
source share
1 answer

Thanks, Thomas worked.

I added ByteArrayHttpMessageConverter to RestTemplate and it worked.

The code I added:

 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; 
+8
source

Source: https://habr.com/ru/post/1201167/


All Articles