title: How to extract HTTP status code from the RestTemplate call to a URL?
body: <p>I am using <code>RestTemplate</code> to make an HTTP call to our service which returns a simple JSON response. I don't need to parse that JSON at all. I just need to return whatever I am getting back from that service.</p>
  
  <p>So I am mapping that to <code>String.class</code> and returning the actual <code>JSON response</code> as a string.</p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate();
  
  String response = restTemplate.getForObject(url, String.class);
  
  return response;
  </code></pre>
  
  <p>Now the question is - </p>
  
  <p>I am trying to extract <code>HTTP Status codes</code> after hitting the URL. How can I extract HTTP Status code from the above code? Do I need to make any change into that in the way I doing it currently?</p>
  
  <p><strong>Update:-</strong></p>
  
  <p>This is what I have tried and I am able to get the response back and status code as well. But do I always need to set <code>HttpHeaders</code> and <code>Entity</code> object like below I am doing it?</p>
  
  <pre><code>    RestTemplate restTemplate = new RestTemplate();     
  
      //and do I need this JSON media type for my use case?
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
  
      //set my entity
      HttpEntity&lt;Object&gt; entity = new HttpEntity&lt;Object&gt;(headers);
  
      ResponseEntity&lt;String&gt; out = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
  
      System.out.println(out.getBody());
      System.out.println(out.getStatusCode());
  </code></pre>
  
  <p>Couple of question -  Do I need to have <code>MediaType.APPLICATION_JSON</code> as I am just making a call to url which returns a response back, it can return either JSON or XML or simple string.</p>