title: Spring Rest Client Exception Handling
body: <p>I am using spring <code>RestTemplate</code> to consume rest services(exposed in spring rest). I am able to consume success scenarios. But for negative scenarios, service returns error messages and error codes. I need to show those error messages in my web page.</p>
  
  <p>For e.g. for invalid request, service throws <code>HttpStatus.BAD_REQUEST</code> with proper messages. If i put try-catch block it goes to catch block and I am not able to get <code>ResponseEntity</code> object.</p>
  
  <pre><code>try {
      ResponseEntity&lt;ResponseWrapper&lt;MyEntity&gt;&gt; responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, entity,
          new ParameterizedTypeReference&lt;ResponseWrapper&lt;MyEntity&gt;&gt;() {
      });
      responseEntity.getStatusCode();
      } catch (Exception e) {
          //TODO How to get response here, so that i can get error messages?
          e.printStackTrace();
      }
  </code></pre>
  
  <p>How to get <code>ResponseWrapper</code> for exception case? </p>
  
  <p>I read about <code>CustomRestTemplate</code> and <code>ResponseExtractor</code> from <a href="https://stackoverflow.com/questions/3322381/spring-resttemplate-behavior-when-handling-responses-with-a-status-of-no-content" title="here">here</a> but could not decide which one is best for my case.</p>