使用resttemplate getforobject时无法捕获错误

ruoxqz4g  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(636)

我正在使用restemplate从我的另一个web服务调用我的web服务的健康执行器端点,以查看web服务是否启动。如果webservice启动,一切正常,但是当它关闭时,我得到一个错误500,“内部服务器错误”。如果我的webservice关闭了,我会尝试捕捉错误以处理它,但是我遇到的问题是我似乎无法捕捉错误。
我试过以下方法,但它从未进入我的任何一个捕获区

@Service
public class DepositService {

  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder
            .setConnectTimeout(Duration.ofMillis(3000))
            .setReadTimeout(Duration.ofMillis(3000))
            .build();
  }

  private static void getBankAccountConnectorHealth() {
    final String uri = "http://localhost:9996/health";

    RestTemplate restTemplate = new RestTemplate();

    String result = null;

    try {
      result = restTemplate.getForObject(uri, String.class);
    } catch (HttpClientErrorException exception) {
      System.out.println("callToRestService Error :" + exception.getResponseBodyAsString());
    } catch (HttpStatusCodeException exception) {
      System.out.println( "callToRestService Error :" + exception.getResponseBodyAsString());
    }

    System.out.println(result);
  }

}

我也试过这样做,但效果一样。它似乎从未进入我的错误处理程序类。

public class NotFoundException extends RuntimeException {
}

public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {

  @Override
  public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
    return (httpResponse.getStatusCode().series() == CLIENT_ERROR || httpResponse.getStatusCode().series() == SERVER_ERROR);
  }

  @Override
  public void handleError(ClientHttpResponse httpResponse) throws IOException {
    if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) {
      // handle SERVER_ERROR
      System.out.println("Server error!");
    } else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) {
      // handle CLIENT_ERROR
      System.out.println("Client error!");

      if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
        throw new NotFoundException();
      }
    }
  }

}

@Service
public class DepositService {

  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder
            .setConnectTimeout(Duration.ofMillis(3000))
            .setReadTimeout(Duration.ofMillis(3000))
            .build();
  }

  private static void getAccountHealth() {
    final String uri = "http://localhost:9996/health";

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());

    String result = null;

    result = restTemplate.getForObject(uri, String.class);

    System.out.println(result);
  }

}

关于如何从另一个Web服务调用我的Web服务的健康执行器并捕获该Web服务是否关闭,有什么建议吗?

1hdlvixo

1hdlvixo1#

看起来像是 getForObject 不会抛出正在捕获的任何异常。从文件上看 RestClientException . 我找到的识别抛出异常的最佳方法是捕获 Exception 并检查它是否有用。
对于第二个方法,我不知道为什么要为 RestTemplate 然后用 new . 你应该注射你的 RestTemplate 并初始化 ResponseErrorHandlerRestTemplateBuilder::errorHandler 方法。

f4t66c6m

f4t66c6m2#

内部发球失误 HttpServerErrorException 如果您想处理这个异常,您应该捕获它,但是更好的方法是使用错误处理程序,您可以看到下面的文章来了解如何做到这一点:
spring rest模板错误处理
spring引导模板错误处理

相关问题