@Bean @ConditionalOnMissingBean public RestTemplateBuilder restTemplateBuilder() { RestTemplateBuilder builder = new RestTemplateBuilder(); HttpMessageConverters converters = this.messageConverters.getIfUnique(); if (converters != null) { builder = builder.messageConverters(converters.getConverters()); } List<RestTemplateCustomizer> customizers = this.restTemplateCustomizers .getIfAvailable(); if (!CollectionUtils.isEmpty(customizers)) { customizers = new ArrayList<RestTemplateCustomizer>(customizers); AnnotationAwareOrderComparator.sort(customizers); builder = builder.customizers(customizers); } return builder; }
@Test public void restTemplateShouldApplyCustomizer() throws Exception { load(RestTemplateCustomizerConfig.class, RestTemplateConfig.class); RestTemplate restTemplate = this.context.getBean(RestTemplate.class); RestTemplateCustomizer customizer = this.context .getBean(RestTemplateCustomizer.class); verify(customizer).customize(restTemplate); }
private void breakBuilderOnNextCall(RestTemplateBuilder builder) { builder.additionalCustomizers(new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { throw new IllegalStateException(); } }); }
/** * Customize the RestTemplate to use Ribbon load balancer to resolve service endpoints */ @Bean public RestTemplateCustomizer ribbonClientRestTemplateCustomizer( final RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory) { return new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { restTemplate.setRequestFactory(ribbonClientHttpRequestFactory); } }; }
public TestRestTemplateConfig( final ObjectProvider<HttpMessageConverters> messageConverters, final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) { super(messageConverters, restTemplateCustomizers); }
@Bean @Order(2) @ConditionalOnBean(OkHttp3ClientHttpRequestFactory.class) public RestTemplateCustomizer okhttp3RestTemplateCustomizer(OkHttp3ClientHttpRequestFactory requestFactory) { return restTemplate -> restTemplate.setRequestFactory(requestFactory); }
public RestTemplateConfiguration( ObjectProvider<HttpMessageConverters> messageConverters, ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) { this.messageConverters = messageConverters; this.restTemplateCustomizers = restTemplateCustomizers; }
@Bean public RestTemplateCustomizer restTemplateCustomizer() { return mock(RestTemplateCustomizer.class); }
@Bean public RestTemplateCustomizer testRestTemplateCustomizer() { return b -> b.setErrorHandler(errorHandler); }
@Bean RestTemplateCustomizer mockMvcCustomizer(BeanFactory beanFactory) { return template -> template.setRequestFactory( beanFactory.getBean(MockMvcClientHttpRequestFactory.class)); }