If you're using spring-web's RestTemplate, here's an example:
try { ResponseEntity<String> response = new RestTemplate().getForEntity(url, String.class); System.out.println("Response: " + response.getBody()); } catch (HttpStatusCodeException e) { System.err.println("Problem querying " + url + ". " + "Status code " + e.getStatusCode() + " and error message " + e.getResponseBodyAsString()); } Note that RestTemplate throws an exception by default for error status codes, so you need to catch HttpStatusCodeException.
Not all errors are HttpStatusCodeException though, but all of them have RestClientException as parent and you can also catch it if really needed.