user1950349 user1950349 - 3 months ago 26
Java Question

Getting InputStream with RestTemplate

I am using URL class to read an InputStream from it. Is there any way I can use RestTemplate for this?

InputStream input = new URL(url).openStream();
JsonReader reader = new JsonReader(new InputStreamReader(input, StandardCharsets.UTF_8.displayName()));


How can I get
InputStream
with
RestTemplate
instead of using
URL
?

Answer

You can't. RestTemplate is meant to encapsulate processing the response (and request) content.

Instead, you can register appropriate HttpMessageConverter objects. Those will have access to the response's InputStream, through a HttpInputMessage object.

Theoretically, you could write a HttpMessageConverter that will retrieve and return the InputStream (or same wrapped version of it), but advise against that.