You can also use restTemplate.execute
and pass ResponseExtractor
that just parses the InputStream
from the body.
public <T> T execute(String url,
org.springframework.http.HttpMethod method,
org.springframework.web.client.RequestCallback requestCallback,
org.springframework.web.client.ResponseExtractor<T> responseExtractor,
Object... uriVariables )
For example:
String rawJson = restTemplate.execute(url, HttpMethod.GET, (clientHttpRequest) -> {}, this::responseExtractor, uriVariables);
// response extractor would be something like this
private String responseExtractor(ClientHttpResponse response) throws IOException {
InputStream inputStream = response.getBody();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
This also bypasses ObjectMapper if your using Jackson and stringify invalid JSON.
String
?restTemplate.getForEntity(url, String.class);
– Brig