package com.bawei.es.config;

import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
        //支持http请求get方式以json对象的形式请求
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
        return restTemplate;
    }

    // 设置超时时间
    public ClientHttpRequestFactory clientHttpRequestFactory(){
        //创建一个httpClient简单工厂
        SimpleClientHttpRequestFactory factory=new SimpleClientHttpRequestFactory();
        //设置连接超时时间,单位ms
        factory.setConnectTimeout(15000);
        //设置读取超时时间,单位ms
        factory.setReadTimeout(10000);
        return factory;
    }
}

