java - 使用带超时的 sping 的 restTemplate,如何检测超时?

标签 java spring-boot timeout resttemplate connection-timeout

我已经按如下方式初始化了我的 restTemplate:

HttpClient httpClient = HttpClientBuilder.create().build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(1000);
requestFactory.setReadTimeout(1000);
restTemplate = new RestTemplate(requestFactory);

我这样调用它:

restTemplate.getForEntity(someString, String.class, SomeHashmapWithURLParameters)

如何处理这两个超时?我假设会抛出异常?如果是这样,我可以捕获哪个特定异常,以便专门处理超时。我正在以不同的方式处理其他异常。

最佳答案

RestTemplate 的情况下,当请求超时时,Spring 将抛出 ResourceAccessException .该实例下的基础异常将是 java.net.SocketTimeoutException,消息为“读取超时”。

关于java - 使用带超时的 sping 的 restTemplate,如何检测超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35808577/

相关文章:

perl - WWW::Mechanize::Timed https 超时不起作用

java - 无法使用 updatetool 安装 javaee7 教程的教程内容

java - 定义常量标识符的最佳方法?

java - 停止窃取 ListView 焦点的按钮

iphone - 没有使用 ASIHTTPRequest 发送请求信息

c# - 在执行长时间运行的数据库任务时处理 Web 服务超时

java - 如何在camel-spring-ws中捕获 SOAP 故障细节

spring - 如何在不继承 spring-boot-starter-web 的情况下在 Spring Boot 中获取 ObjectMapper 实例?

java - 在 Spring Boot 中使用多个调度程序 Servlet/Web 上下文

java - 从 OkHttp Response Java 获取值