本文整理汇总了Java中org.springframework.web.client.RestTemplate.setInterceptors方法的典型用法代码示例。如果您正苦于以下问题:Java RestTemplate.setInterceptors方法的具体用法?Java RestTemplate.setInterceptors怎么用?Java RestTemplate.setInterceptors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.setInterceptors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private void init(ApplicationContext ctx) {
loadBalancedRestTemplate = new RestTemplate();
SpringClientFactory springClientFactory = springClientFactory();
springClientFactory.setApplicationContext(ctx);
loadBalancerClient = new RibbonLoadBalancerClient(springClientFactory);
//custom restTemplate
LoadBalancerRequestFactory requestFactory = new LoadBalancerRequestFactory(loadBalancerClient, Collections.emptyList());
LoadBalancerInterceptor interceptor = new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
List<ClientHttpRequestInterceptor> interceptors = loadBalancedRestTemplate.getInterceptors();
ArrayList<ClientHttpRequestInterceptor> customedInterceptors = new ArrayList<>(interceptors.size() + 1);
customedInterceptors.addAll(interceptors);
customedInterceptors.add(interceptor);
loadBalancedRestTemplate.setInterceptors(customedInterceptors);
}
示例2: 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;
}
示例3: TracingRestTemplateTest
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public TracingRestTemplateTest() {
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(
new TracingRestTemplateInterceptor(mockTracer)));
client = new Client<RestTemplate>() {
@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> clazz) {
return restTemplate.getForEntity(url, clazz);
}
@Override
public RestTemplate template() {
return restTemplate;
}
};
mockServer = MockRestServiceServer.bindTo(client.template()).build();
}
示例4: run
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
@SuppressWarnings("checkstyle:emptyblock")
public void run() {
LOG.warn("****** SelfRevokingProvider Thread going to sleep for 15 secs");
try {
Thread.sleep(THREAD_SLEEP);
} catch (InterruptedException ie) {
}
LOG.warn("****** SelfRevokingProvider finished sleeping, revoking all tokens");
try {
RestTemplate rt = restManager.getRestTemplate("keystone-revoke");
rt.setInterceptors(Collections.singletonList(this));
LOG.warn("****** deleting " + keystoneTokenURI.toString());
for (TokenHolder tokenHolder : TokenManager.getInstance().getAllTokenHolders()) {
currentToken = tokenHolder.getUnscoped();
LOG.warn("****** deleting token " + currentToken + "[" + keystoneTokenURI.toString() + "]");
rt.delete(keystoneTokenURI);
}
} catch (HttpClientErrorException ex) {
LOG.error("****** token revocation refused for token " + currentToken);
}
LOG.warn("****** all tokens have been revoked - SelfRevokingProvider Thread is exiting...");
}
示例5: restTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Bean
public RestTemplate restTemplate(OmegaContext context) {
RestTemplate template = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
interceptors.add(new TransactionClientHttpRequestInterceptor(context));
template.setInterceptors(interceptors);
return template;
}
示例6: testErrorUnknownHostException
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
public void testErrorUnknownHostException() {
String url = "http://nonexisting.example.com";
try {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(
new TracingRestTemplateInterceptor(mockTracer)));
restTemplate.getForEntity(url, String.class);
} catch (ResourceAccessException ex) {
//ok UnknownHostException
}
List<MockSpan> mockSpans = mockTracer.finishedSpans();
Assert.assertEquals(1, mockSpans.size());
MockSpan mockSpan = mockSpans.get(0);
Assert.assertEquals("GET", mockSpan.operationName());
Assert.assertEquals(5, mockSpan.tags().size());
Assert.assertEquals(RestTemplateSpanDecorator.StandardTags.COMPONENT_NAME,
mockSpan.tags().get(Tags.COMPONENT.getKey()));
Assert.assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
Assert.assertEquals("GET", mockSpan.tags().get(Tags.HTTP_METHOD.getKey()));
Assert.assertEquals(url, mockSpan.tags().get(Tags.HTTP_URL.getKey()));
Assert.assertEquals(Boolean.TRUE, mockSpan.tags().get(Tags.ERROR.getKey()));
Assert.assertEquals(1, mockSpan.logEntries().size());
Assert.assertEquals(2, mockSpan.logEntries().get(0).fields().size());
Assert.assertEquals(Tags.ERROR.getKey(), mockSpan.logEntries().get(0).fields().get("event"));
Assert.assertNotNull(mockSpan.logEntries().get(0).fields().get("error.object"));
}
示例7: registerTracingInterceptor
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private void registerTracingInterceptor(RestTemplate restTemplate) {
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
for (ClientHttpRequestInterceptor interceptor: interceptors) {
if (interceptor instanceof TracingRestTemplateInterceptor) {
return;
}
}
log.info("Adding " + TracingRestTemplateInterceptor.class.getSimpleName() + " to rest template");
interceptors = new ArrayList<>(interceptors);
interceptors.add(new TracingRestTemplateInterceptor(tracer));
restTemplate.setInterceptors(interceptors);
}
示例8: customize
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Override
public void customize(RestTemplate restTemplate) {
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
templateHandler = this.interceptor.createUriTemplateHandler(templateHandler);
restTemplate.setUriTemplateHandler(templateHandler);
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(this.interceptor);
interceptors.addAll(restTemplate.getInterceptors());
restTemplate.setInterceptors(interceptors);
}
示例9: getRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Gets a Rest Template.
*
* @return A RestTemplate for rest requests.
*/
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// Request Interceptors
restTemplate.setInterceptors(new ArrayList<ClientHttpRequestInterceptor>());
restTemplate.getInterceptors().addAll(getRestInterceptors());
// Default Error Handler
restTemplate.setErrorHandler(new RestErrorHandler());
return restTemplate;
}
示例10: setRestTemplateInterceptor
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private void setRestTemplateInterceptor(final RestTemplate restTemplate) {
restTemplate.setInterceptors(Collections.singletonList(
new TraceRestTemplateInterceptor(this.tracer, new ZipkinHttpSpanInjector(),
new HttpTraceKeysInjector(this.tracer, new TraceKeys()))));
}
示例11: 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());
}
示例12: getRestTemplateWithToken
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public RestTemplate getRestTemplateWithToken() {
RestTemplate rt = restManager.getRestTemplate("keystone-data");
rt.setInterceptors(Collections.singletonList(this));
return rt;
}
示例13: getRestTemplateWithUnscopedToken
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get a RestTemplate with an unscoped token (via the interceptor).
*
* @return the rest template
*/
public RestTemplate getRestTemplateWithUnscopedToken() {
RestTemplate rt = restManager.getRestTemplate("keystone-data");
rt.setInterceptors(Collections.singletonList(new UnscopedClientHttpRequestInterceptor(tokenManager)));
return rt;
}
示例14: getRestTemplateWithUnscopedAndSubjectToken
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get a RestTemplate with an UnscopedAndSubjectToken token (via the interceptor).
*
* @return the rest template
*/
public RestTemplate getRestTemplateWithUnscopedAndSubjectToken() {
RestTemplate rt = restManager.getRestTemplate("keystone-data");
rt.setInterceptors(Collections.singletonList(new UnscopedAndSubjectClientHttpRequestInterceptor(tokenManager)));
return rt;
}
示例15: getRestTemplateWithScopedToken
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get a RestTemplate with an scoped token (via the interceptor).
*
* @param projectId the project id
* @return the rest template
*/
public RestTemplate getRestTemplateWithScopedToken(final String projectId) {
RestTemplate rt = restManager.getRestTemplate("scoped");
rt.setInterceptors(Collections.singletonList(new ScopedClientHttpRequestInterceptor(projectId, tokenManager)));
return rt;
}