Java Code Examples for org.springframework.web.client.RestClientResponseException

Following code examples demonstrate how to use org.springframework.web.client.RestClientResponseExceptionfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.client.RestClientResponseExceptionand various code implementation of this class.

	public static Optional<String> getResponseBodyAsString(RestClientException rce) {
		if (rce.getClass().isAssignableFrom(RestClientResponseException.class)) {
			return Optional.ofNullable(((RestClientResponseException) rce).getResponseBodyAsString());
		}
		return Optional.empty();
	} 


  public Optional<Member> getMember(String memberId) {
    try {
      ResponseEntity<Member> response = memberServiceClient.getMember(memberId);
      return Optional.of(response.getBody());
    } catch (RestClientResponseException ex) {
      if (ex.getRawStatusCode() == 404) {
        return Optional.empty();
      }
      log.error("Could not find member {} , {}", memberId, ex);
      throw ex;
    }
  } 

    public static AwesomeException from(RestClientResponseException ex) {
        Gson gson = new Gson();
        ErrorMessage errorMessage = gson.fromJson(ex.getResponseBodyAsString(),
                ErrorMessage.class);
        return new AwesomeException(ex.getRawStatusCode(),
                errorMessage.code, errorMessage.message);
    } 

Advertisement
Javadoc
Common base class for exceptions that contain actual HTTP response data. @author Rossen Stoyanchev @

since 4.3

Read More
Advertisement