Provide web client Auto-configuration
Add auto-configuration for use with typical web clients. Currently client auto-configuration consists of a RestTemplateBuilder pre-configured with HttpMessageConverters. Closes gh-5507pull/6081/head
parent
b641e63466
commit
b38231021d
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.boot.web.client.RestTemplateCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for web client.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class)
|
||||
public class WebClientAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(RestTemplate.class)
|
||||
public static class RestTemplateConfiguration {
|
||||
|
||||
private final ObjectProvider<HttpMessageConverters> messageConverters;
|
||||
|
||||
private final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers;
|
||||
|
||||
public RestTemplateConfiguration(
|
||||
ObjectProvider<HttpMessageConverters> messageConverters,
|
||||
ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
|
||||
this.messageConverters = messageConverters;
|
||||
this.restTemplateCustomizers = restTemplateCustomizers;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.boot.web.client.RestTemplateCustomizer;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link WebClientAutoConfiguration}
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class WebClientAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restTemplateWhenMessageConvertersDefinedShouldHaveMessageConverters() {
|
||||
load(HttpMessageConvertersAutoConfiguration.class, RestTemplateConfig.class);
|
||||
assertThat(this.context.getBeansOfType(RestTemplate.class)).hasSize(1);
|
||||
RestTemplate restTemplate = this.context.getBean(RestTemplate.class);
|
||||
List<HttpMessageConverter<?>> converters = this.context
|
||||
.getBean(HttpMessageConverters.class).getConverters();
|
||||
assertThat(restTemplate.getMessageConverters())
|
||||
.containsExactlyElementsOf(converters);
|
||||
assertThat(restTemplate.getRequestFactory())
|
||||
.isInstanceOf(HttpComponentsClientHttpRequestFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restTemplateWhenNoMessageConvertersDefinedShouldHaveDefaultMessageConverters() {
|
||||
load(RestTemplateConfig.class);
|
||||
RestTemplate restTemplate = this.context.getBean(RestTemplate.class);
|
||||
assertThat(restTemplate.getMessageConverters().size())
|
||||
.isEqualTo(new RestTemplate().getMessageConverters().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restTemplateWhenHasCustomMesssageConvertersShouldHaveMessageConverters() {
|
||||
load(CustomHttpMessageConverter.class,
|
||||
HttpMessageConvertersAutoConfiguration.class, RestTemplateConfig.class);
|
||||
RestTemplate restTemplate = this.context.getBean(RestTemplate.class);
|
||||
List<Class<?>> converterClasses = new ArrayList<Class<?>>();
|
||||
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
|
||||
converterClasses.add(converter.getClass());
|
||||
}
|
||||
assertThat(converterClasses).contains(CustomHttpMessageConverter.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restTemplateWhenHasCustomBuilderShouldUseCustomBuilder() {
|
||||
load(RestTemplateConfig.class, CustomRestTemplateBuilderConfig.class);
|
||||
assertThat(this.context.getBeansOfType(RestTemplate.class)).hasSize(1);
|
||||
RestTemplate restTemplate = this.context.getBean(RestTemplate.class);
|
||||
assertThat(restTemplate.getMessageConverters()).hasSize(1);
|
||||
assertThat(restTemplate.getMessageConverters().get(0))
|
||||
.isInstanceOf(CustomHttpMessageConverter.class);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderShouldBeFreshForEachUse() throws Exception {
|
||||
load(DirtyRestTemplateConfig.class);
|
||||
}
|
||||
|
||||
public void load(Class<?>... config) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(config);
|
||||
ctx.register(WebClientAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(RestTemplateBuilder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class DirtyRestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplateOne(RestTemplateBuilder builder) {
|
||||
try {
|
||||
return builder.build();
|
||||
}
|
||||
finally {
|
||||
breakBuilderOnNextCall(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplateTwo(RestTemplateBuilder builder) {
|
||||
try {
|
||||
return builder.build();
|
||||
}
|
||||
finally {
|
||||
breakBuilderOnNextCall(builder);
|
||||
}
|
||||
}
|
||||
|
||||
private void breakBuilderOnNextCall(RestTemplateBuilder builder) {
|
||||
builder.additionalCustomizers(new RestTemplateCustomizer() {
|
||||
|
||||
@Override
|
||||
public void customize(RestTemplate restTemplate) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomRestTemplateBuilderConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplateBuilder restTemplateBuilder() {
|
||||
return new RestTemplateBuilder()
|
||||
.messageConverters(new CustomHttpMessageConverter());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class RestTemplateCustomizerConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplateCustomizer restTemplateCustomizer() {
|
||||
return mock(RestTemplateCustomizer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class CustomHttpMessageConverter extends StringHttpMessageConverter {
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue