Java Code Examples for org.springframework.http.client.HttpComponentsClientHttpRequestFactory#setBufferRequestBody()
The following examples show how to use
org.springframework.http.client.HttpComponentsClientHttpRequestFactory#setBufferRequestBody() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomRestTemplate.java From hesperides with GNU General Public License v3.0 | 6 votes |
public CustomRestTemplate(Gson gson, UriTemplateHandler uriTemplateHandler, CloseableHttpClient httpClient) { super(); this.gson = gson; HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); if (httpClient != null) { requestFactory.setHttpClient(httpClient); } requestFactory.setBufferRequestBody(false); setRequestFactory(requestFactory); setUriTemplateHandler(uriTemplateHandler); setErrorHandler(new NoOpResponseErrorHandler()); // On vire Jackson: List<HttpMessageConverter<?>> converters = getMessageConverters().stream() .filter(httpMessageConverter -> !(httpMessageConverter instanceof MappingJackson2HttpMessageConverter)) .collect(Collectors.toList()); configureMessageConverters(converters, gson); setMessageConverters(converters); }
Example 2
Source File: RestTemplateWrapper.java From lite-tracer with Apache License 2.0 | 5 votes |
/** * 获取包装trace信息之后的RestTemplate * @return */ public RestTemplate getRestTemlate() { // 使用拦截器包装http header RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(new ArrayList<ClientHttpRequestInterceptor>() { { add((request, body, execution) -> { String traceId = RequestContext.getTraceId(); String spanId = RequestContext.getSpanId(); String parentSpanId = RequestContext.getParentSpanId(); if (StringUtils.isNotEmpty(traceId)) { request.getHeaders().add(Constants.HTTP_HEADER_TRACE_ID, traceId); } if (StringUtils.isNotEmpty(spanId)) { request.getHeaders().add(Constants.HTTP_HEADER_SPAN_ID, spanId); } if (StringUtils.isNotEmpty(parentSpanId)) { request.getHeaders().add(Constants.HTTP_HEADER_PARENT_SPAN_ID, parentSpanId); } return execution.execute(request, body); }); } }); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); // 注意此处需开启缓存,否则会报getBodyInternal方法“getBody not supported”错误 factory.setBufferRequestBody(true); restTemplate.setRequestFactory(factory); return restTemplate; }
Example 3
Source File: RestTemplateConfig.java From LicenseDemo with Apache License 2.0 | 4 votes |
/** * ClientHttpRequestFactory接口的另一种实现方式(推荐使用),即: * HttpComponentsClientHttpRequestFactory:底层使用Httpclient连接池的方式创建Http连接请求 * @return HttpComponentsClientHttpRequestFactory */ @Bean public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){ //Httpclient连接池,长连接保持30秒 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS); //设置总连接数 connectionManager.setMaxTotal(1000); //设置同路由的并发数 connectionManager.setDefaultMaxPerRoute(1000); //设置header List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04")); headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate")); headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")); headers.add(new BasicHeader("Connection", "keep-alive")); //创建HttpClient HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultHeaders(headers) .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) //设置重试次数 .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) //设置保持长连接 .build(); //创建HttpComponentsClientHttpRequestFactory实例 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); //设置客户端和服务端建立连接的超时时间 requestFactory.setConnectTimeout(5000); //设置客户端从服务端读取数据的超时时间 requestFactory.setReadTimeout(5000); //设置从连接池获取连接的超时时间,不宜过长 requestFactory.setConnectionRequestTimeout(200); //缓冲请求数据,默认为true。通过POST或者PUT大量发送数据时,建议将此更改为false,以免耗尽内存 requestFactory.setBufferRequestBody(false); return requestFactory; }
Example 4
Source File: RestTemplateConfig.java From LicenseDemo with Apache License 2.0 | 4 votes |
/** * ClientHttpRequestFactory接口的另一种实现方式(推荐使用),即: * HttpComponentsClientHttpRequestFactory:底层使用Httpclient连接池的方式创建Http连接请求 * @return HttpComponentsClientHttpRequestFactory */ @Bean public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){ //Httpclient连接池,长连接保持30秒 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS); //设置总连接数 connectionManager.setMaxTotal(1000); //设置同路由的并发数 connectionManager.setDefaultMaxPerRoute(1000); //设置header List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04")); headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate")); headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")); headers.add(new BasicHeader("Connection", "keep-alive")); //创建HttpClient HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultHeaders(headers) .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) //设置重试次数 .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) //设置保持长连接 .build(); //创建HttpComponentsClientHttpRequestFactory实例 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); //设置客户端和服务端建立连接的超时时间 requestFactory.setConnectTimeout(5000); //设置客户端从服务端读取数据的超时时间 requestFactory.setReadTimeout(5000); //设置从连接池获取连接的超时时间,不宜过长 requestFactory.setConnectionRequestTimeout(200); //缓冲请求数据,默认为true。通过POST或者PUT大量发送数据时,建议将此更改为false,以免耗尽内存 requestFactory.setBufferRequestBody(false); return requestFactory; }