本文整理汇总了Java中org.springframework.web.client.RestTemplate.setRequestFactory方法的典型用法代码示例。如果您正苦于以下问题:Java RestTemplate.setRequestFactory方法的具体用法?Java RestTemplate.setRequestFactory怎么用?Java RestTemplate.setRequestFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.setRequestFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static RestTemplate getTemplate(ClientHttpRequestInterceptor interceptor) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
ris.add(interceptor);
restTemplate.setInterceptors(ris);
SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
httpFactory.setOutputStreaming(false);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpFactory));
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
示例2: afterPropertiesSet
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public void afterPropertiesSet() throws UnsupportedEncodingException {
Collection<Header> defaultHeaders = new ArrayList<Header>();
Header header = new BasicHeader("Authorization",
"Basic " + BaseEncoding.base64().encode("apollo:".getBytes("UTF-8")));
defaultHeaders.add(header);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("apollo", ""));
CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
.setDefaultHeaders(defaultHeaders).build();
restTemplate = new RestTemplate(httpMessageConverters.getConverters());
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(portalConfig.connectTimeout());
requestFactory.setReadTimeout(portalConfig.readTimeout());
restTemplate.setRequestFactory(requestFactory);
}
示例3: disableSSL
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static void disableSSL(RestTemplate restTemplate) {
try {
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));
} catch (Exception e) {
log.error("Exception occurred while creating http factory, error={}", e.getMessage(), e);
}
}
示例4: buildRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Build the RestTemplate used to make HTTP requests.
* @return RestTemplate
*/
protected RestTemplate buildRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// This allows us to read the response more than once - Necessary for debugging.
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
return restTemplate;
}
示例5: createRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public RestTemplate createRestTemplate(HttpProxyConfiguration httpProxyConfiguration, boolean
trustSelfSignedCerts) {
RestTemplate restTemplate = new LoggingRestTemplate();
restTemplate.setRequestFactory(createRequestFactory(httpProxyConfiguration, trustSelfSignedCerts));
restTemplate.setErrorHandler(new CloudControllerResponseErrorHandler());
restTemplate.setMessageConverters(getHttpMessageConverters());
return restTemplate;
}
示例6: assertNetworkCallFails
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private void assertNetworkCallFails(RestTemplate restTemplate, ClientHttpRequestFactory requestFactory) {
restTemplate.setRequestFactory(requestFactory);
try {
HttpStatus status = restTemplate.execute(CCNG_API_URL + "/info", HttpMethod.GET, null, new ResponseExtractor<HttpStatus>() {
public HttpStatus extractData(ClientHttpResponse response) throws IOException {
return response.getStatusCode();
}
});
Assert.fail("Expected byteman rules to detect direct socket connections, status is:" + status);
} catch (Exception e) {
// good, byteman rejected it as expected
// e.printStackTrace();
}
assertEquals("Not expecting Jetty to receive requests since we asked direct connections", 0, nbInJvmProxyRcvReqs.get());
}
示例7: setUp
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Before
public void setUp()
{
Log log = mock(Log.class);
when(log.isDebugEnabled()).thenReturn(true);
loggingCustomizer = new LoggingCustomizer(log);
restTemplate = new RestTemplate();
restTemplate.setRequestFactory(requestFactory);
}
示例8: update
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Async
public void update(final String keycloakToken) {
RestTemplate template = new KeycloakRestTemplate(keycloakToken);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
template.setRequestFactory(requestFactory);
ResponseEntity<String> response = template.exchange(updateTenantEndpoint, HttpMethod.PATCH, null, String.class);
LOG.info("User tenant has been updated. Status code {}", response.getStatusCode());
}
示例9: checkIfServerAlreadyRunning
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Checks if the data server is already running, by calling the /health endpoint.
* @return <code>true</code> if running
*/
private static boolean checkIfServerAlreadyRunning() {
logger.info("In DataServerManager.checkIfServerAlreadyRunning");
try {
// issue a GET to Spring Boot health endpoint to determine if data server is already running...
// (it returns a short JSON reply message)
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(500); // set short connect timeout
requestFactory.setReadTimeout(2000); // set slightly longer read timeout
restTemplate.setRequestFactory(requestFactory);
String result = restTemplate.getForObject("http://localhost:9999/health", String.class);
logger.info("Server is already running, received {}", result);
return true;
} catch (RestClientException ex) {
logger.info("Server not already running");
return false;
}
}
示例10: initRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* springboot初始化restTemplate
*/
@Bean
public RestTemplate initRestTemplate() {
// 长连接保持30秒
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS);
// 总连接数
pollingConnectionManager.setMaxTotal(1000);
// 同路由的并发数
pollingConnectionManager.setDefaultMaxPerRoute(1000);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重试次数,默认是3次,没有开启
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));
// 保持长连接配置,需要在头添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
RequestConfig.Builder builder = RequestConfig.custom();
builder.setConnectionRequestTimeout(200);
builder.setConnectTimeout(5000);
builder.setSocketTimeout(5000);
RequestConfig requestConfig = builder.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
httpClientBuilder.setDefaultHeaders(headers);
HttpClient httpClient = httpClientBuilder.build();
// httpClient连接配置,底层是配置RequestConfig
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时
clientHttpRequestFactory.setConnectTimeout(5000);
// 数据读取超时时间,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(5000);
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
clientHttpRequestFactory.setConnectionRequestTimeout(200);
// 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。
clientHttpRequestFactory.setBufferRequestBody(false);
// 添加内容转换器
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(initStringHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
RestTemplate restTemplate = new RestTemplate(messageConverters);
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
示例11: getRawRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private RestTemplate getRawRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(
new ByteArrayHttpMessageConverter());
restTemplate.setRequestFactory(
new InterceptingClientHttpRequestFactory(
restTemplate.getRequestFactory(),
Collections.singletonList(
new BasicAuthorizationInterceptor("1", "1"))));
return restTemplate;
}
示例12: prepareRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Ensures that the passed-in {@link RestTemplate} is using the Apache HTTP Client. If the optional {@code username} AND
* {@code password} are not empty, then a {@link BasicCredentialsProvider} will be added to the {@link CloseableHttpClient}.
*
* Furthermore, you can set the underlying {@link SSLContext} of the {@link HttpClient} allowing you to accept self-signed
* certificates.
*
* @param restTemplate Must not be null
* @param username Can be null
* @param password Can be null
* @param skipSslValidation Use with caution! If true certificate warnings will be ignored.
*/
public static void prepareRestTemplate(
RestTemplate restTemplate,
String username,
String password,
boolean skipSslValidation) {
Assert.notNull(restTemplate, "The provided RestTemplate must not be null.");
final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(
username,
password));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
if (skipSslValidation) {
httpClientBuilder.setSSLContext(HttpClientUtils.buildCertificateIgnoringSslContext());
httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
}
final CloseableHttpClient httpClient = httpClientBuilder.build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate.setRequestFactory(requestFactory);
}
示例13: configureRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Configure a {@link RestTemplate} for communication with a CredHub server.
* @param restTemplate an existing {@link RestTemplate} to configure
* @param baseUri the base URI for the CredHub server
* @param clientHttpRequestFactory the {@link ClientHttpRequestFactory} to use when
* creating new connections
*/
public static void configureRestTemplate(RestTemplate restTemplate, String baseUri,
ClientHttpRequestFactory clientHttpRequestFactory) {
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setUriTemplateHandler(createUriTemplateHandler(baseUri));
restTemplate.setMessageConverters(createMessageConverters());
restTemplate.setInterceptors(createInterceptors());
}
示例14: configureCloudFoundryRequestFactory
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
protected void configureCloudFoundryRequestFactory(RestTemplate restTemplate) {
ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
if (!(requestFactory instanceof CloudFoundryClientHttpRequestFactory)) {
restTemplate.setRequestFactory(new CloudFoundryClientHttpRequestFactory(requestFactory));
}
}
示例15: customize
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
public void customize(RestTemplate restTemplate)
{
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
restTemplate.getInterceptors().add(new LoggingInterceptor(log));
}