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

后端 未结 1 1433
我在风中等你
我在风中等你 2021-02-15 15:31

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

1条回答
  •  [愿得一人]
    2021-02-15 16:07

    Thanks Thomas it worked.

    I added ByteArrayHttpMessageConverter to the RestTemplate and it worked.

    Code I added:

    ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    
    List supportedApplicationTypes = new ArrayList<>();
    MediaType pdfApplication = new MediaType("application","pdf");
    supportedApplicationTypes.add(pdfApplication);
    
    byteArrayHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
    List> messageConverters = new ArrayList<>();
    messageConverters.add(byteArrayHttpMessageConverter);
    restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(messageConverters);
    
    Object result = getRestTemplate().getForObject(url, returnClass, parameters);
    byte[] resultByteArr = (byte[])result;
    

    0 讨论(0)
提交回复
热议问题