title: Response Entity is null if the HTTPStatus Code is not equal to 200
body: <p>I just started to use the Spring Framework and making some tests. I'm trying to reach ResponseEntity. But if the HTTP status code is not equal to 200, the ResponseEntity object is always null. Therefore I cannot reach the status code or headers.</p>
  
  <p>Is there any way to reach ResponseEntity if the HTTP status code is not equal to 200?</p>
  
  <pre><code>public static String checkPatientExistence(String pNo, String eNo) throws RestClientException, NullPointerException {
      String result = null;
      RestTemplate restTemplate = createRestTemplate();
      final String uri = apiURL + "GetInstance";
  
      UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri)
              .queryParam("pNo", pNo)
              .queryParam("eNo", eNo);
  
      HttpHeaders headers = new HttpHeaders();
      headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
  
      HttpEntity&lt;String&gt; requestEntity = new HttpEntity&lt;&gt;(headers);
  
      ResponseEntity&lt;String&gt; responseEntity = null;
      try {
          responseEntity = restTemplate.exchange(
                  builder.toUriString(),
                  HttpMethod.GET,
                  requestEntity,
                  String.class
          );
      } catch (RestClientException restClientException) {
          System.out.println(restClientException.getMessage());
          System.out.println(Arrays.toString(restClientException.getStackTrace()));
      }
  
      if (responseEntity != null) {
          if (responseEntity.getStatusCode() == HttpStatus.OK) {
              result = "true";
          } else if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
              System.out.println(responseEntity.getStatusCode().toString());
              System.out.println(responseEntity.getBody());
              result = "false";
          } else if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
              System.out.println(responseEntity.getStatusCode().toString());
              System.out.println(responseEntity.getBody());
              result = "noContent";
          }
      } else if (responseEntity == null) {
          System.out.println("responseEntity is null");
          result = "false";
      }
      return result;
  }
  </code></pre>
  
  <p>Here is the output of the method:</p>
  
  <pre><code>400 Bad Request: [{"Message":"error message"}]
  [org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101),
  org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170),
  org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112),
  org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63),
  org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782),
  org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740),
  org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674),
  org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583),
  com.xxx.xxx.App.checkPatientExistence(App.java:269), com.xxx.xxx.App.main(App.java:43)]
  responseEntity is null
  false
  
  </code></pre>