在Spring Rest Template中设置超时 [英] Setting timeouts in Spring Rest Template
问题描述
应用程序使用Spring rest模板来调用webservice,我使用
restTemplate.exchage(url)来调用webservice。
目前我们没有为此webservice调用传递任何超时值,如何为Spring Rest模板设置超时值。
Application is using Spring rest template to call a webservice and i am using
restTemplate.exchage(url) to call the webservice.
Currently we are not passing any timeout value for this webservice call, How can i set a timeout value for Spring Rest template.
推荐答案
您可以使用类似于以下的代码来设置连接超时:
You can use code similar to following for setting connection timeout:
RestTemplate restTemplate = new RestTemplate();
((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(2000);
如果您希望设置读取超时,则可以使用类似以下的代码:
If your wish to set read timeout, you can have code similar to following:
((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(2000);
此处以毫秒为单位给出时间。有关详细信息,请访问文档页面。
The time is given in milliseconds here. For more info, you can visit the documentation page.
这篇关于在Spring Rest Template中设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!