title: How to parse gzip encoded response with RestTemplate in Spring-Web
body: <p>After I modified <a href="https://spring.io/guides/gs/consuming-rest/" rel="noreferrer">Consuming a RESTful Web Service</a> example to call <a href="https://api.stackexchange.com/docs/users-by-ids">get users by id</a> from api.stackexchange.com I get JsonParseException:</p>
  
  <p><code>com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens</code></p>
  
  <p>Response from <code>api.stackexchange.com</code> is gzip compressed. </p>
  
  <p><strong>How to add support for gzip compressed response into Spring-Web RestTemplate?</strong></p>
  
  <p>I am using Spring boot parent ver. <code>1.3.1.RELEASE</code> hence Spring-Web <code>4.2.4-RELEASE</code></p>
  
  <p>Here’s my adjusted example:</p>
  
  <p>User.java</p>
  
  <pre><code>package stackexchange.dto;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
  import com.fasterxml.jackson.databind.annotation.JsonNaming;
  
  @JsonIgnoreProperties(ignoreUnknown = true)
  @JsonNaming(LowerCaseWithUnderscoresStrategy.class)
  public class User {
  
      // Properties made public in order to shorten the example
      public int userId;
      public String displayName;
      public int reputation;
  
      @Override
      public String toString() {
          return "user{"
                  + "display_name='" + displayName + '\''
                  + "reputation='" + reputation + '\''
                  + "user_id='" + userId + '\''
                  + '}';
      }
  }
  </code></pre>
  
  <p>CommonWrapper.java</p>
  
  <pre><code>package stackexchange.dto;
  
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
  import com.fasterxml.jackson.databind.annotation.JsonNaming;
  
  @JsonIgnoreProperties(ignoreUnknown = true)
  @JsonNaming(LowerCaseWithUnderscoresStrategy.class)
  public class CommonWrapper {
  
      // Properties made public in order to shorten the example
      public boolean hasMore;
      // an array of the type found in type
      public User[] items;
      public int page;
      public int pageSize;
      public int quotaMax;
      public int quotaRemaining;
  
      @Override
      public String toString() {
          StringBuilder sb = new StringBuilder();
          for (User user : items) {
              sb.append("{" + user.toString() + "}\n");
          }
  
          return "common_wrapper{"
          + "\"items\"=[\n"
          + sb
          + "]"
          + "has_more='" + hasMore + '\''
          + "page='" + page + '\''
          + "page_size='" + pageSize + '\''
          + "quota_max='" + quotaMax + '\''
          + "quota_remaining='" + quotaRemaining + '\''
          + '}';
      }
  }
  </code></pre>
  
  <p>StackExchange.java</p>
  
  <pre><code>package stackexchange;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.web.client.RestTemplate;
  
  import stackexchange.dto.CommonWrapper;
  
  import com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy;
  import com.fasterxml.jackson.databind.annotation.JsonNaming;
  
  @JsonNaming(LowerCaseWithUnderscoresStrategy.class)
  public class StackExchange implements CommandLineRunner{
  
      private static final Logger log = LoggerFactory.getLogger(StackExchange.class);
  
      public static void main(String args[]) {
          SpringApplication.run(StackExchange.class);
      }
  
      @Override
      public void run(String... strings) throws Exception {
  
          RestTemplate restTemplate = new RestTemplate();
          CommonWrapper response = restTemplate
                  .getForObject(
                          "https://api.stackexchange.com/2.2/users/4607349?site=stackoverflow",
                          CommonWrapper.class);
  
          log.info(response.toString());
      }
  
  }
  </code></pre>
  
  <p>pom.xml - same as in example</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;stackexchangetest&lt;/groupId&gt;
    &lt;artifactId&gt;stackexchangetest&lt;/artifactId&gt;
    &lt;version&gt;0.0.1&lt;/version&gt;
    &lt;name&gt;stackexchangetest&lt;/name&gt;
    &lt;description&gt;api.stackexchange.com Test&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.3.1.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;/dependency&gt;   
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>