Allow for RestTemplate customization

This commit reuses the `RestTemplateBuilder` infrastructure wherever
a `RestTemplate` is required. This allows to customize the rest template
if necessary.

Closes gh-481
This commit is contained in:
Stephane Nicoll
2017-08-14 11:26:34 +02:00
parent 70d224b072
commit 1bf0d0fcde
7 changed files with 263 additions and 40 deletions

View File

@@ -18,6 +18,7 @@ package io.spring.initializr.web.autoconfigure;
import java.util.ArrayList;
import java.util.List;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
@@ -43,12 +44,14 @@ import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.resource.ResourceUrlProvider;
/**
@@ -64,7 +67,7 @@ import org.springframework.web.servlet.resource.ResourceUrlProvider;
*/
@Configuration
@EnableConfigurationProperties(InitializrProperties.class)
@AutoConfigureAfter(CacheAutoConfiguration.class)
@AutoConfigureAfter({ CacheAutoConfiguration.class, WebClientAutoConfiguration.class })
public class InitializrAutoConfiguration {
private final List<ProjectRequestPostProcessor> postProcessors;
@@ -75,30 +78,6 @@ public class InitializrAutoConfiguration {
this.postProcessors = list != null ? list : new ArrayList<>();
}
@Bean
public WebConfig webConfig() {
return new WebConfig();
}
@Bean
@ConditionalOnMissingBean
public MainController initializrMainController(
InitializrMetadataProvider metadataProvider,
TemplateRenderer templateRenderer,
ResourceUrlProvider resourceUrlProvider,
ProjectGenerator projectGenerator,
DependencyMetadataProvider dependencyMetadataProvider) {
return new MainController(metadataProvider, templateRenderer, resourceUrlProvider
, projectGenerator, dependencyMetadataProvider);
}
@Bean
@ConditionalOnMissingBean
public UiController initializrUiController(
InitializrMetadataProvider metadataProvider) {
return new UiController(metadataProvider);
}
@Bean
@ConditionalOnMissingBean
public ProjectGenerator projectGenerator() {
@@ -130,10 +109,12 @@ public class InitializrAutoConfiguration {
@Bean
@ConditionalOnMissingBean(InitializrMetadataProvider.class)
public InitializrMetadataProvider initializrMetadataProvider(
InitializrProperties properties) {
InitializrProperties properties,
RestTemplateBuilder restTemplateBuilder) {
InitializrMetadata metadata = InitializrMetadataBuilder
.fromInitializrProperties(properties).build();
return new DefaultInitializrMetadataProvider(metadata, new RestTemplate());
return new DefaultInitializrMetadataProvider(metadata,
restTemplateBuilder.build());
}
@Bean
@@ -142,9 +123,40 @@ public class InitializrAutoConfiguration {
return new DefaultDependencyMetadataProvider();
}
@Configuration
@ConditionalOnWebApplication
static class InitializrWebConfiguration {
@Bean
public WebConfig webConfig() {
return new WebConfig();
}
@Bean
@ConditionalOnMissingBean
public MainController initializrMainController(
InitializrMetadataProvider metadataProvider,
TemplateRenderer templateRenderer,
ResourceUrlProvider resourceUrlProvider,
ProjectGenerator projectGenerator,
DependencyMetadataProvider dependencyMetadataProvider) {
return new MainController(metadataProvider, templateRenderer, resourceUrlProvider
, projectGenerator, dependencyMetadataProvider);
}
@Bean
@ConditionalOnMissingBean
public UiController initializrUiController(
InitializrMetadataProvider metadataProvider) {
return new UiController(metadataProvider);
}
}
@Configuration
@ConditionalOnClass(javax.cache.CacheManager.class)
static class CacheConfiguration {
static class InitializrCacheConfiguration {
@Bean
public JCacheManagerCustomizer initializrCacheManagerCustomizer() {

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2012-2017 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 io.spring.initializr.web.autoconfigure;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link InitializrAutoConfiguration}.
*
* @author Stephane Nicoll
*/
public class InitializrAutoConfigurationTests {
private ConfigurableApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void customRestTemplateBuilderIsUsed() {
load(CustomRestTemplateConfiguration.class);
assertThat(this.context.getBeansOfType(InitializrMetadataProvider.class))
.hasSize(1);
RestTemplate restTemplate = (RestTemplate) new DirectFieldAccessor(
this.context.getBean(InitializrMetadataProvider.class))
.getPropertyValue("restTemplate");
assertThat(restTemplate.getErrorHandler()).isSameAs(
CustomRestTemplateConfiguration.errorHandler);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(ctx, environment);
if (config != null) {
ctx.register(config);
}
ctx.register(WebClientAutoConfiguration.class, InitializrAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration
static class CustomRestTemplateConfiguration {
private static final ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
@Bean
public RestTemplateCustomizer testRestTemplateCustomizer() {
return b -> b.setErrorHandler(errorHandler);
}
}
}