RestTemplate基本配置类
# RestTemplateConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* RestTemplate注入,配置超时时间
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate() {
// 配置HTTP超时时间为5s
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(5000);
httpRequestFactory.setReadTimeout(5000);
return new RestTemplate(httpRequestFactory);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编辑 (opens new window)
上次更新: 2023/05/17, 22:21:48
- 01
- SpringCache基本配置类05-16
- 03
- Rpamis-security-原理解析12-13