1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.http.client.ClientHttpRequestFactory;
    4. import org.springframework.http.client.SimpleClientHttpRequestFactory;
    5. import org.springframework.http.converter.StringHttpMessageConverter;
    6. import org.springframework.web.client.RestTemplate;
    7. import java.nio.charset.Charset;
    8. @Configuration
    9. public class RestTemplateConfig {
    10. /**
    11. * 加载RestTemplate
    12. *
    13. * @param simpleClientHttpRequestFactory
    14. * @return
    15. */
    16. @Bean
    17. public RestTemplate restTemplate(ClientHttpRequestFactory simpleClientHttpRequestFactory) {
    18. RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
    19. restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("GBK")));
    20. return restTemplate;
    21. }
    22. /**
    23. * 设置参数
    24. *
    25. * @return
    26. */
    27. @Bean
    28. public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
    29. SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    30. factory.setConnectTimeout(15000);
    31. factory.setReadTimeout(5000);
    32. return factory;
    33. }
    34. }