Search This Blog

Wednesday, November 20, 2013

Exception handling with Spring RestTemplate


Exception handling with Spring RestTemplate

<T> ResponseEntity<String> doPost( String uri, 
                                   T request, 
                                   HttpStatus expectedStatus) 
{
    ResponseEntity<String> response = null;
    try 
    {
        response = _restTemplate.postForEntity(uri, request, String.class);
    } 
    catch (HttpStatusCodeException e)  // thrown by DefaultResponseErrorHandler
    { 
       // Server Response: 40x , 50x
    } 
    catch (ResourceAccessException e) // thrown by RestTemplate.doExecute
    { 
        // I/O error, including marshaling
    }
    catch (Exception e) 
    { 
        // other error
    }
    return response;
}