After I modified Consuming a RESTful Web Service example to call get users by id from api.stackexchange.com I get JsonParseException:
com.fasterxml.jackson.cor
private String callViaRest(String requestString, Steps step) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_XML);
headers.add("Accept-Encoding", "application/gzip");
HttpEntity<String> entity = new HttpEntity<String>(requestString, headers);
byte[] responseBytes = jsonRestTemplate
.exchange("yourUrl", HttpMethod.POST, entity, byte[].class).getBody();
String decompressed = null;
try {
decompressed= new String(CompressionUtil.decompressGzipByteArray(responseBytes),Charsets.UTF_8);
} catch (IOException e) {
LOGGER.error("network call failed.", e);
}
return decompressed;
}
Replace the default requestFactory with one from Apache HttpClient (which decodes GZIP on the fly):
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
Add Apache Http Client into pom.xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!--Version is not needed when used with Spring Boot parent pom file -->
<version>4.5.1</version>
</dependency>