title: Spring RestTemplate, getting junk response when http status code is 404
body: <p>I am writing a rest proxy (it exposes the API and delegates call to other server) and it works fine for the normal case and also for 500 http status code, we get the response from the rest client.
  But when we get 404 status code, the Rest API server returns the message but we get junk values from the RestTemplate. We need to pass the same response to other API user but cannot get the same response.</p>
  
  <p><strong>Message returned from REST API Server:</strong></p>
  
  <hr>
  
  <p>{
    "status_code":"0",
    "error":{<br>
       "code":"404",
       "description":"Source not found"
      }
  }</p>
  
  <p><strong>Getting the below response by RestTemplate client:</strong></p>
  
  <p>Not able to paste the content, attaching the screen shot of the response.
  <img src="https://i.stack.imgur.com/H5VGf.png" alt="Response for Http 404 error">
  Please see the code below.</p>
  
  <pre><code>@RequestMapping(value = "/api/**")
  public @ResponseBody String apiProxy(@RequestBody String body, HttpMethod method, HttpServletRequest request,
          HttpServletResponse response) throws URISyntaxException {
      RestTemplate restTemplate = new RestTemplate(
              new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
      restTemplate.setInterceptors(Collections.singletonList(new RestClientLoggingInterceptor()));
      restTemplate.setErrorHandler(new CustomResponseErrorHandler());
      restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
  
      HttpHeaders httpHeaders = new HttpHeaders();
      Enumeration&lt;String&gt; headers = request.getHeaderNames();
      String headerName = null;
      String headerValue = null;
      while (headers.hasMoreElements()) {
          headerName = headers.nextElement();
          headerValue = request.getHeader(headerName);
          httpHeaders.set(headerName, headerValue);
      }
      HttpEntity&lt;String&gt; httpEntity = new HttpEntity&lt;String&gt;(body, httpHeaders);
  
      URI uri = new URI(ServerProtocol, null, ServerDomain, Integer.valueOf(ServerPort),
              request.getRequestURI(), request.getQueryString(), null);
  
      ResponseEntity&lt;String&gt; responseEntity = null;
      try {
          responseEntity = restTemplate.exchange(uri, method, httpEntity, String.class);
      } catch (RestClientResponseException e) {
          response.setStatus(e.getRawStatusCode());
          return e.getResponseBodyAsString();
      }
      response.setStatus(responseEntity.getStatusCode().value());
      return responseEntity.getBody();
  }
  </code></pre>
  
  <p>ResponseErrorHandler Class</p>
  
  <pre><code>   public class CustomResponseErrorHandler extends DefaultResponseErrorHandler {
  
          private static final Logger logger = LogManager.getLogger(CustomResponseErrorHandler.class);
  
          @Override
          public void handleError(ClientHttpResponse response) throws IOException {
              logger.error("Response error: {} {}", response.getStatusCode(), response.getStatusText());
          }
  
      }
  </code></pre>
  
  <p>RestClientLoggingInterceptor Class</p>
  
  <pre><code>@Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
                      throws IOException {
                  ClientHttpResponse response = execution.execute(request, body);
                  logger.debug("request method:" + request.getMethod());
                  logger.debug("request URI:" + request.getURI());
                  logger.debug("request headers:" + request.getHeaders());
                  logger.debug("request body:" + new String(body, Charset.forName("UTF-8")));
                  logger.debug("response status code:" + response.getStatusCode());
                  logger.debug("response headers:" + response.getHeaders());
                  logger.debug("response body:" + IOUtils.toString(response.getBody(), "UTF-8"));
  
                  return response;
              }
  </code></pre>
  
  <p>Thanks</p>