org.springframework.web.client.RestTemplate.getInterceptors()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(200)

本文整理了Java中org.springframework.web.client.RestTemplate.getInterceptors方法的一些代码示例,展示了RestTemplate.getInterceptors的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestTemplate.getInterceptors方法的具体详情如下:
包路径:org.springframework.web.client.RestTemplate
类名称:RestTemplate
方法名:getInterceptors

RestTemplate.getInterceptors介绍

暂无

代码示例

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

private boolean hasTraceInterceptor(RestTemplate restTemplate) {
  for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
    if (interceptor instanceof TracingClientHttpRequestInterceptor) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: liuyangming/ByteTCC

@org.springframework.context.annotation.Primary
@org.springframework.context.annotation.Bean
public RestTemplate restTemplate(@Autowired CompensableRequestInterceptor compensableRequestInterceptor) {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getInterceptors().add(compensableRequestInterceptor);
  return restTemplate;
}

代码示例来源:origin: liuyangming/ByteTCC

@org.springframework.context.annotation.Primary
@org.springframework.context.annotation.Bean
public RestTemplate restTemplate(@Autowired CompensableRequestInterceptor compensableRequestInterceptor) {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getInterceptors().add(compensableRequestInterceptor);
  return restTemplate;
}

代码示例来源: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: liuyangming/ByteTCC

@org.springframework.context.annotation.Primary
@org.springframework.cloud.client.loadbalancer.LoadBalanced
@org.springframework.context.annotation.Bean
public RestTemplate defaultRestTemplate(@Autowired CompensableRequestInterceptor compensableRequestInterceptor) {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getInterceptors().add(compensableRequestInterceptor);
  return restTemplate;
}

代码示例来源: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: liuyangming/ByteTCC

@org.springframework.context.annotation.Primary
@org.springframework.cloud.client.loadbalancer.LoadBalanced
@org.springframework.context.annotation.Bean
public RestTemplate defaultRestTemplate(@Autowired CompensableRequestInterceptor compensableRequestInterceptor) {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.getInterceptors().add(compensableRequestInterceptor);
  return restTemplate;
}

代码示例来源: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: 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: spring-cloud-incubator/spring-cloud-alibaba

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
    throws BeansException {
  if (cache.containsKey(beanName)) {
    // add interceptor for each RestTemplate with @SentinelRestTemplate annotation
    StringBuilder interceptorBeanName = new StringBuilder();
    SentinelRestTemplate sentinelRestTemplate = cache.get(beanName);
    interceptorBeanName
        .append(StringUtils.uncapitalize(
            SentinelProtectInterceptor.class.getSimpleName()))
        .append("_")
        .append(sentinelRestTemplate.blockHandlerClass().getSimpleName())
        .append(sentinelRestTemplate.blockHandler()).append("_")
        .append(sentinelRestTemplate.fallbackClass().getSimpleName())
        .append(sentinelRestTemplate.fallback());
    RestTemplate restTemplate = (RestTemplate) bean;
    registerBean(interceptorBeanName.toString(), sentinelRestTemplate);
    SentinelProtectInterceptor sentinelProtectInterceptor = applicationContext
        .getBean(interceptorBeanName.toString(),
            SentinelProtectInterceptor.class);
    restTemplate.getInterceptors().add(0, sentinelProtectInterceptor);
  }
  return bean;
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

@Override
public void customize(RestTemplate restTemplate) {
  if (this.zipkinProperties.getCompression().isEnabled()) {
    restTemplate.getInterceptors().add(0, new GZipInterceptor());
  }
}

代码示例来源: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: org.springframework.boot/spring-boot

restTemplate.getInterceptors().add(this.basicAuthentication);
restTemplate.getInterceptors().addAll(this.interceptors);
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
  for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) {

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testRestTemplateHasInterceptor() throws Exception {
  final List<RestTemplate> restTemplates = Arrays.asList(
      new RestTemplate(),
      new RestTemplate(new SimpleClientHttpRequestFactory()),
      new RestTemplate(Collections.singletonList(new StringHttpMessageConverter())));
  for (RestTemplate restTemplate : restTemplates) {
    assertThat(restTemplate.getInterceptors()).hasSize(1);
    assertThat(restTemplate.getInterceptors()).hasAtLeastOneElementOfType(SpringRestTemplateContextPropagatingInterceptor.class);
  }
}

代码示例来源: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: 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: Nepxion/Discovery

@Bean
@LoadBalanced
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = MySentinelExceptionHandler.class)
public RestTemplate restTemplate(@Autowired(required = false) RestTemplateStrategyInterceptor restTemplateStrategyInterceptor) {
  RestTemplate restTemplate = new RestTemplate();
  if (restTemplateStrategyInterceptor != null) {
    restTemplate.getInterceptors().add(restTemplateStrategyInterceptor);
  }
  return restTemplate;
}

代码示例来源: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: stackoverflow.com

@Bean
public RestTemplate restTemplate() {

  final RestTemplate restTemplate = new RestTemplate();

  restTemplate.setMessageConverters(Arrays.asList(
      new FormHttpMessageConverter(),
      new StringHttpMessageConverter()
  ));
  restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("client", "secret"));

  return restTemplate;
}

相关文章