目录
POST请求,对ServletInputStream的读取配置
引入依赖包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
RestTemplate配置类
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Configuration
public class RestTemplateConfig {
@Resource
private CustomHttpMessageConverter customHttpMessageConverter;
/**
* 初探RestTemplate--解决中文乱码问题
* https://blog.csdn.net/mryang125/article/details/80963280
* @return
*/
@Bean
public RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = httpRequestFactory();
RestTemplate restTemplate = new RestTemplate(factory);
// 可以添加消息转换 restTemplate.setMessageConverters(...);
restTemplate.getMessageConverters().set(5,customHttpMessageConverter);//解决请求返回数据restTemplate内部解析数据异常
restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
// 可以增加拦截器
//restTemplate.setInterceptors(...);
//解决中文乱码问题
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> httpMessageConverter : list) {
if(httpMessageConverter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(StandardCharsets.UTF_8);
break;
}
}
return restTemplate;
}
private HttpComponentsClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(restTemplateConfigHttpClient());
}
private HttpClient restTemplateConfigHttpClient() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
// 设置整个连接池最大连接数 根据自己的场景决定
connectionManager.setMaxTotal(200);
// 路由是对maxTotal的细分
connectionManager.setDefaultMaxPerRoute(50);
RequestConfig requestConfig = RequestConfig.custom()
// 服务器返回数据(response)的时间,超过该时间抛出read timeout
.setSocketTimeout(30000)
// 连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
.setConnectTimeout(5000)