本文整理了Java中org.springframework.web.client.RestTemplate.setInterceptors
方法的一些代码示例,展示了RestTemplate.setInterceptors
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestTemplate.setInterceptors
方法的具体详情如下:
包路径:org.springframework.web.client.RestTemplate
类名称:RestTemplate
方法名:setInterceptors
暂无
代码示例来源:origin: eugenp/tutorials
public void setDebugging(boolean debugging) {
List<ClientHttpRequestInterceptor> currentInterceptors = this.restTemplate.getInterceptors();
if(debugging) {
if (currentInterceptors == null) {
currentInterceptors = new ArrayList<ClientHttpRequestInterceptor>();
}
ClientHttpRequestInterceptor interceptor = new ApiClientHttpRequestInterceptor();
currentInterceptors.add(interceptor);
this.restTemplate.setInterceptors(currentInterceptors);
} else {
if (currentInterceptors != null && !currentInterceptors.isEmpty()) {
Iterator<ClientHttpRequestInterceptor> iter = currentInterceptors.iterator();
while (iter.hasNext()) {
ClientHttpRequestInterceptor interceptor = iter.next();
if (interceptor instanceof ApiClientHttpRequestInterceptor) {
iter.remove();
}
}
this.restTemplate.setInterceptors(currentInterceptors);
}
}
this.debugging = debugging;
}
代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba
@PostConstruct
public void init() {
if (this.restTemplates != null) {
for (RestTemplate restTemplate : restTemplates) {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
restTemplate.getInterceptors());
interceptors.add(this.fescarRestTemplateInterceptor);
restTemplate.setInterceptors(interceptors);
}
}
}
代码示例来源:origin: stackoverflow.com
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
代码示例来源:origin: codingapi/tx-lcn
@Override
public void customize(RestTemplate restTemplate) {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors());
if(loadBalancerInterceptor!=null) {
list.add(0, loadBalancerInterceptor);
}
if(retryLoadBalancerInterceptor!=null){
list.add(0, retryLoadBalancerInterceptor);
}
restTemplate.setInterceptors(list);
}
代码示例来源:origin: javamelody/javamelody
/** {@inheritDoc} */
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
// RestTemplate et getInterceptors() existent depuis spring-web 3.1.0.RELEASE
if (REST_TEMPLATE_INTERCEPTOR_AVAILABLE && bean instanceof RestTemplate) {
final RestTemplate restTemplate = (RestTemplate) bean;
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
restTemplate.getInterceptors());
for (final ClientHttpRequestInterceptor interceptor : interceptors) {
if (interceptor instanceof SpringRestTemplateInterceptor) {
return bean;
}
}
interceptors.add(SpringRestTemplateInterceptor.SINGLETON);
restTemplate.setInterceptors(interceptors);
}
return bean;
}
代码示例来源:origin: spring-cloud/spring-cloud-sleuth
void inject(RestTemplate restTemplate) {
if (hasTraceInterceptor(restTemplate)) {
return;
}
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
restTemplate.getInterceptors());
interceptors.add(0, this.interceptor);
restTemplate.setInterceptors(interceptors);
}
代码示例来源:origin: openzipkin/brave
@Override protected RestTemplate newClient(HttpTracing httpTracing) {
OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok);
RestTemplate result = new RestTemplate(factory);
result.setInterceptors(Collections.singletonList(
TracingClientHttpRequestInterceptor.create(httpTracing
)));
return result;
}
代码示例来源:origin: stackoverflow.com
RestTemplate rt = new RestTemplate();
//set interceptors/requestFactory
ClientHttpRequestInterceptor ri = new LoggingRequestInterceptor();
List<ClientHttpRequestInterceptor> ris = new ArrayList<ClientHttpRequestInterceptor>();
ris.add(ri);
rt.setInterceptors(ris);
rt.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
代码示例来源:origin: QNJR-GROUP/EasyTransaction
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);
}
代码示例来源:origin: spring-cloud/spring-cloud-config
private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
if (client.getRequestReadTimeout() < 0) {
throw new IllegalStateException("Invalid Value for Read Timeout set.");
}
requestFactory.setReadTimeout(client.getRequestReadTimeout());
RestTemplate template = new RestTemplate(requestFactory);
Map<String, String> headers = new HashMap<>(client.getHeaders());
if (headers.containsKey(AUTHORIZATION)) {
headers.remove(AUTHORIZATION); // To avoid redundant addition of header
}
if (!headers.isEmpty()) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(
new GenericRequestHeaderInterceptor(headers)));
}
return template;
}
代码示例来源:origin: spring-projects/spring-security-oauth
protected RestOperations getRestTemplate() {
if (restTemplate == null) {
synchronized (this) {
if (restTemplate == null) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(getResponseErrorHandler());
restTemplate.setRequestFactory(requestFactory);
restTemplate.setInterceptors(interceptors);
this.restTemplate = restTemplate;
}
}
}
if (messageConverters == null) {
setMessageConverters(new RestTemplate().getMessageConverters());
}
return restTemplate;
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(Collections.singletonList(interceptor));
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(Collections.singletonList(interceptor));
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(contentType);
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-14694
public void repeatedAccessToResponseViaResource() {
Resource resource = new ClassPathResource("ludwig.json", this.getClass());
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
.ignoreExpectOrder(true)
.bufferContent() // enable repeated reads of response body
.build();
mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));
restTemplate.getForObject("/composers/{id}", Person.class, 42);
mockServer.verify();
}
代码示例来源:origin: apache/servicecomb-java-chassis
@Override
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
super.setInterceptors(interceptors);
defaultRestTemplate.setInterceptors(interceptors);
}
代码示例来源:origin: org.springframework.cloud/spring-cloud-commons
@Bean
@ConditionalOnMissingBean
public RestTemplateCustomizer restTemplateCustomizer(
final LoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(
restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
}
代码示例来源:origin: org.springframework.cloud/spring-cloud-commons
@Bean
@ConditionalOnMissingBean
public RestTemplateCustomizer restTemplateCustomizer(
final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(
restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
}
代码示例来源:origin: apache/servicecomb-saga
@Bean(name = "omegaRestTemplate")
public RestTemplate omegaRestTemplate(@Autowired(required=false) OmegaContext context) {
RestTemplate template = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
interceptors.add(new TransactionClientHttpRequestInterceptor(context));
template.setInterceptors(interceptors);
return template;
}
}
代码示例来源:origin: org.springframework.boot/spring-boot-actuator
@Override
public void customize(RestTemplate restTemplate) {
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
templateHandler = this.interceptor.createUriTemplateHandler(templateHandler);
restTemplate.setUriTemplateHandler(templateHandler);
List<ClientHttpRequestInterceptor> existingInterceptors = restTemplate
.getInterceptors();
if (!existingInterceptors.contains(this.interceptor)) {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(this.interceptor);
interceptors.addAll(existingInterceptors);
restTemplate.setInterceptors(interceptors);
}
}
代码示例来源:origin: org.apache.servicecomb/provider-springmvc
@Override
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
super.setInterceptors(interceptors);
defaultRestTemplate.setInterceptors(interceptors);
}
内容来源于网络,如有侵权,请联系作者删除!