Skip to content

Commit 425e5a0

Browse files
committed
Add RestTemplate constructor with custom converters
Prior to this commit, RestTemplate's constructors were all initializing default HTTPMessageConverters. Its API provides a way to replace those converters with custom ones, but default converters are already defined and initialized at that point, which can be an issue in some cases (performance, classpath...). This commits adds a new constructor for RestTemplate with a list of message converters as argument. With this new constructor, default message converters are never initialized. Issue: SPR-11351
1 parent fcbd3b1 commit 425e5a0

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -115,6 +115,7 @@
115115
* requestFactory} and {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties.
116116
*
117117
* @author Arjen Poutsma
118+
* @author Brian Clozel
118119
* @since 3.0
119120
* @see HttpMessageConverter
120121
* @see RequestCallback
@@ -141,13 +142,14 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
141142

142143
private final ResponseExtractor<HttpHeaders> headersExtractor = new HeadersExtractor();
143144

144-
private List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
145+
private final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
145146

146147
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
147148

148149

149150
/**
150151
* Create a new instance of the {@link RestTemplate} using default settings.
152+
* Default {@link HttpMessageConverter}s are initialized.
151153
*/
152154
@SuppressWarnings("deprecation")
153155
public RestTemplate() {
@@ -182,14 +184,26 @@ public RestTemplate(ClientHttpRequestFactory requestFactory) {
182184
setRequestFactory(requestFactory);
183185
}
184186

187+
/**
188+
* Create a new instance of the {@link RestTemplate} using the given list of
189+
* {@link HttpMessageConverter} to use
190+
* @param messageConverters the list of {@link HttpMessageConverter} to use
191+
* @since 4.0.1
192+
*/
193+
public RestTemplate(List<HttpMessageConverter<?>> messageConverters) {
194+
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
195+
this.messageConverters.addAll(messageConverters);
196+
}
197+
185198

186199
/**
187200
* Set the message body converters to use.
188201
* <p>These converters are used to convert from and to HTTP requests and responses.
189202
*/
190203
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
191204
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
192-
this.messageConverters = messageConverters;
205+
this.messageConverters.clear();
206+
this.messageConverters.addAll(messageConverters);
193207
}
194208

195209
/**

spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,9 +68,9 @@ public void setUp() {
6868
response = mock(ClientHttpResponse.class);
6969
errorHandler = mock(ResponseErrorHandler.class);
7070
converter = mock(HttpMessageConverter.class);
71-
template = new RestTemplate(requestFactory);
71+
template = new RestTemplate(Collections.<HttpMessageConverter<?>>singletonList(converter));
72+
template.setRequestFactory(requestFactory);
7273
template.setErrorHandler(errorHandler);
73-
template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
7474
}
7575

7676
@Test

0 commit comments

Comments
 (0)