Skip to main content
added 23 characters in body
Source Link
cahen
  • 16.9k
  • 15
  • 51
  • 83

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.

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, 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.

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.

added 2 characters in body
Source Link
cahen
  • 16.9k
  • 15
  • 51
  • 83

If you're using Spring's 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" +  "Status code " + e.getStatusCode() +  " and error message " + e.getResponseBodyAsString()); } 

Note that RestTemplate throws an exception by default, 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 needneeded.

If you're using Spring'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, 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 need.

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, 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.

Source Link
cahen
  • 16.9k
  • 15
  • 51
  • 83

If you're using Spring'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, 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 need.