From 425e5a066e3769cc304ce6cd34971a6c38b9a4bb Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 28 Jan 2014 11:33:45 +0100 Subject: [PATCH] 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 --- .../web/client/RestTemplate.java | 20 ++++++++++++++++--- .../web/client/RestTemplateTests.java | 6 +++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 7e73ef53a58..86a24d1a1bb 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -115,6 +115,7 @@ import org.springframework.web.util.UriTemplate; * requestFactory} and {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties. * * @author Arjen Poutsma + * @author Brian Clozel * @since 3.0 * @see HttpMessageConverter * @see RequestCallback @@ -141,13 +142,14 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat private final ResponseExtractor headersExtractor = new HeadersExtractor(); - private List> messageConverters = new ArrayList>(); + private final List> messageConverters = new ArrayList>(); private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); /** * Create a new instance of the {@link RestTemplate} using default settings. + * Default {@link HttpMessageConverter}s are initialized. */ @SuppressWarnings("deprecation") public RestTemplate() { @@ -182,6 +184,17 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat setRequestFactory(requestFactory); } + /** + * Create a new instance of the {@link RestTemplate} using the given list of + * {@link HttpMessageConverter} to use + * @param messageConverters the list of {@link HttpMessageConverter} to use + * @since 4.0.1 + */ + public RestTemplate(List> messageConverters) { + Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); + this.messageConverters.addAll(messageConverters); + } + /** * Set the message body converters to use. @@ -189,7 +202,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat */ public void setMessageConverters(List> messageConverters) { Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); - this.messageConverters = messageConverters; + this.messageConverters.clear(); + this.messageConverters.addAll(messageConverters); } /** diff --git a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java index fcfc3407704..63215051978 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -68,9 +68,9 @@ public class RestTemplateTests { response = mock(ClientHttpResponse.class); errorHandler = mock(ResponseErrorHandler.class); converter = mock(HttpMessageConverter.class); - template = new RestTemplate(requestFactory); + template = new RestTemplate(Collections.>singletonList(converter)); + template.setRequestFactory(requestFactory); template.setErrorHandler(errorHandler); - template.setMessageConverters(Collections.>singletonList(converter)); } @Test