package com.ruoyi.yunxin.util;

import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class RestTemplateUtil {

    public static RestTemplate restTemplate;

    static {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(3000);
        requestFactory.setReadTimeout(3000);
        restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        //添加转换器
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter instanceof StringHttpMessageConverter) {
                StringHttpMessageConverter converter = (StringHttpMessageConverter) messageConverter;
                converter.setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
    }

}
