whyclxw
2024-11-07 afbc8273269cdff4d4e55549b8f08257a7a83b64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.whyc.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
 
@Configuration
public class HttpConifg {
 
    @Bean
    @Scope("prototype")
    public RestTemplate getRestTemplate(){
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        // 配置RestTemplate不自动处理重定向
        /*HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setBufferRequestBody(false); // 不要求缓冲请求体
        requestFactory.setFollowRedirects(false); // 不自动处理重定向*/
        // 设置连接超时时间(单位:毫秒)
        requestFactory.setConnectTimeout(5000);
        // 设置请求超时时间(单位:毫秒)
        requestFactory.setReadTimeout(10000);
        return new RestTemplate(requestFactory);
    }
 
}