开发者

Springboot RestTemplate设置超时时间的方法(Spring boot 版本)

开发者 https://www.devze.com 2024-08-12 10:30 出处:网络 作者: wtopps
Java Config方式 Spring boot 版本<=1.3 @Configuration public claspythons AppConfig{ @Bean public RestTemplate customRestTemplate(){

Java Config方式

Spring boot 版本<=1.3

@Configuration
public claspythons AppConfig{
    @Bean
    public RestTemplate customRestTemplate(){
        HandroidttpComponentsClientHttpRequestFactory 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
@Configphpuration
public class AppConfig{
    @Bean
    @ConfigurationProperties(prefix = "custom.rest.connection")
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
        return new HttpComponbsWVhVfUGentsClientHttpRequestFactory();
    }
    @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超时时间内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(编程客栈www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消