This commit is contained in:
鲨鱼
2024-12-21 00:21:28 +08:00
parent 10a5f1c903
commit 583fb29acc
15 changed files with 712 additions and 57 deletions

View File

@@ -0,0 +1,35 @@
package com.ruoyi.cai.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);
}
}
}
public static RestTemplate getRest(){
return restTemplate;
}
}