Skip to content

Customize RestTemplateBuilder #22896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration.NotReactiveWebApplicationCondition;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateBuilderCustomizer;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.boot.web.client.RestTemplateRequestCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -59,6 +61,7 @@ public class RestTemplateAutoConfiguration {
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConverters> messageConverters,
ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
ObjectProvider<RestTemplateBuilderCustomizer> restTemplateBuilderCustomizer,
ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers) {
RestTemplateBuilder builder = new RestTemplateBuilder();
HttpMessageConverters converters = messageConverters.getIfUnique();
Expand All @@ -67,6 +70,10 @@ public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConvert
}
builder = addCustomizers(builder, restTemplateCustomizers, RestTemplateBuilder::customizers);
builder = addCustomizers(builder, restTemplateRequestCustomizers, RestTemplateBuilder::requestCustomizers);
for (RestTemplateBuilderCustomizer customizer : restTemplateBuilderCustomizer) {
builder = customizer.customize(builder);
Assert.notNull(builder, "RestTemplateBuilderCustomizer returned null builder");
}
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import java.util.List;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateBuilderCustomizer;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.boot.web.client.RestTemplateRequestCustomizer;
import org.springframework.context.annotation.Bean;
Expand All @@ -42,10 +45,12 @@
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.doThrow;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;

/**
* Tests for {@link RestTemplateAutoConfiguration}
Expand Down Expand Up @@ -157,6 +162,25 @@ void whenReactiveWebApplicationRestTemplateBuilderIsNotConfigured() {
.run((context) -> assertThat(context).doesNotHaveBean(RestTemplateBuilder.class));
}

@Test
void customizerShouldCustomizeBuilder() {
this.contextRunner.withUserConfiguration(RestTemplateBuilderCustomizerConfig.class).run((context) ->
assertThatThrownBy(() -> context.getBean(RestTemplateBuilder.class).build())
.isInstanceOf(IllegalStateException.class).hasMessageContaining("customized builder")
);
}

@Test
void customizerShouldReturnInstanceNotANull() {
this.contextRunner.withUserConfiguration(RestTemplateBuilderCustomizerReturnsNullConfig.class).run((context) ->
assertThatThrownBy(() -> context.getBean(RestTemplateBuilder.class).build())
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining("Error creating bean with name 'restTemplateBuilder'")
.hasRootCauseInstanceOf(IllegalArgumentException.class)
.hasRootCauseMessage("RestTemplateBuilderCustomizer returned null builder")
);
}

@Configuration(proxyBeanMethods = false)
static class RestTemplateConfig {

Expand Down Expand Up @@ -232,4 +256,28 @@ static class CustomHttpMessageConverter extends StringHttpMessageConverter {

}

@Configuration(proxyBeanMethods = false)
static class RestTemplateBuilderCustomizerConfig {

@Bean
RestTemplateBuilderCustomizer restTemplateBuilderCustomizer() {
return (oldBuilder) -> {
final RestTemplateBuilder builder = Mockito.spy(oldBuilder);
doThrow(new IllegalStateException("customized builder")).when(builder).build();
return builder;
};
}

}

@Configuration(proxyBeanMethods = false)
static class RestTemplateBuilderCustomizerReturnsNullConfig {

@Bean
RestTemplateBuilderCustomizer restTemplateBuilderCustomizer() {
return (oldBuilder) -> null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2012-2019 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
*
* https://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.web.client;

/**
* Callback interface that can be used to customize a {@link RestTemplateBuilder}.
*
* @author Ivo Smid
* @see RestTemplateBuilder
* @since 5.3.0
*/
@FunctionalInterface
public interface RestTemplateBuilderCustomizer {

/**
* Callback to customize a {@link RestTemplateBuilder} instance.
* @param restTemplateBuilder the original builder to customize
* @return customized builder instance
*/
RestTemplateBuilder customize(RestTemplateBuilder restTemplateBuilder);

}