【问题标题】:Setting a read timeout for RestTemplate为 RestTemplate 设置读取超时
【发布时间】:2015-08-26 17:42:31
【问题描述】:

我正在使用 Spring RestTemplate 从我的应用程序向不同的 REST 端点发出简单的 POST 请求。目前我在 Spring 配置文件中设置 readTimout 如下所示:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
            <property name="readTimeout" value="10000" />
            <property name="connectTimeout" value="10000"/>
        </bean>
    </constructor-arg>
</bean>

最近我的任务是要求根据每个请求动态设置 readTimout

我想我每次发出 POST 请求时都必须使用新的超时值将新的 RequestFactory 注入到 restTemplate 中,但这是一种可以接受的方法吗?有没有更好的办法?

【问题讨论】:

    标签: spring rest timeout resttemplate


    【解决方案1】:

    每次我发现我可以直接在 HttpComponentsClientHttpRequestFactory 对象上显式设置读取超时,而不是每次都将新的 RequestFactory 注入到 restTemplate 中。

    ((HttpComponentsClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(timeout)

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 2020-12-20
      • 2018-10-21
      • 2014-12-04
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      相关资源
      最近更新 更多