java config方式
spring boot 版本<=1.3
@configuration
public class appconfig{
@bean
public resttemplate customresttemplate(){
httpcomponentsclienthttprequestfactory httprequestfactory = new httpcomponentsclienthttprequestfactory();
httprequestfactory.setconnectionrequesttimeout(3000);
httprequestfactory.setconnecttimeout(3000);
httprequestfactory.setreadtimeout(3000);
return new resttemplate(httprequestfactory);
}
}配置文件方式指定
custom.rest.connection.connection-request-timeout=3000 custom.rest.connection.connect-timeout=3000 custom.rest.connection.read-timeout=3000
@configuration
public class appconfig{
@bean
@configurationproperties(prefix = "custom.rest.connection")
public httpcomponentsclienthttprequestfactory customhttprequestfactory() {
return new httpcomponentsclienthttprequestfactory();
}
@bean
public resttemplate customresttemplate(){
return new resttemplate(customhttprequestfactory());
}
}spring boot 版本>=1.4
@configuration
public class appconfig
{
@bean
public resttemplate resttemplate(resttemplatebuilder resttemplatebuilder)
{
return resttemplatebuilder
.setconnecttimeout(...)
.setreadtimeout(...)
.build();
}
}到此这篇关于springboot resttemplate设置超时时间的文章就介绍到这了,更多相关springboot resttemplate超时时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论