diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConverters.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConverters.java index 8aad3563a5..e62b3163b5 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConverters.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConverters.java @@ -23,10 +23,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; -import org.springframework.beans.factory.InitializingBean; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter; -import org.springframework.util.Assert; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** @@ -47,15 +45,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp * @see #HttpMessageConverters(HttpMessageConverter...) * @see #HttpMessageConverters(Collection) * @see #getConverters() - * @see #setConverters(List) */ -public class HttpMessageConverters implements Iterable>, - InitializingBean { +public class HttpMessageConverters implements Iterable> { private List> converters; - private boolean initialized; - /** * Create a new {@link HttpMessageConverters} instance with the specified additional * converters. @@ -77,18 +71,19 @@ public class HttpMessageConverters implements Iterable>, * converter manipulation. */ public HttpMessageConverters(Collection> additionalConverters) { - this.converters = new ArrayList>(); + List> converters = new ArrayList>(); List> defaultConverters = getDefaultConverters(); for (HttpMessageConverter converter : additionalConverters) { int defaultConverterIndex = indexOfItemClass(defaultConverters, converter); if (defaultConverterIndex == -1) { - this.converters.add(converter); + converters.add(converter); } else { defaultConverters.set(defaultConverterIndex, converter); } } - this.converters.addAll(defaultConverters); + converters.addAll(defaultConverters); + this.converters = Collections.unmodifiableList(converters); } private List> getDefaultConverters() { @@ -139,19 +134,4 @@ public class HttpMessageConverters implements Iterable>, return this.converters; } - /** - * Set the converters to use, replacing any existing values. This method can only be - * called before the bean has been initialized. - * @param converters the converters to set - */ - public void setConverters(List> converters) { - Assert.state(!this.initialized, "Unable to set converters once initialized"); - this.converters = converters; - } - - @Override - public void afterPropertiesSet() throws Exception { - this.initialized = true; - this.converters = Collections.unmodifiableList(this.converters); - } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersTests.java index 821f7ccf0c..d7759af130 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersTests.java @@ -64,37 +64,6 @@ public class HttpMessageConvertersTests { Jaxb2RootElementHttpMessageConverter.class))); } - @Test - public void canModifyBeforeInitialize() throws Exception { - HttpMessageConverters converters = new HttpMessageConverters(); - HttpMessageConverter converter = mock(HttpMessageConverter.class); - converters.getConverters().add(converter); - assertThat(converters.getConverters().contains(converter), equalTo(true)); - } - - @Test - public void cannotModifyAfterInitialize() throws Exception { - HttpMessageConverters converters = new HttpMessageConverters(); - converters.afterPropertiesSet(); - this.thrown.expect(UnsupportedOperationException.class); - converters.getConverters().add(mock(HttpMessageConverter.class)); - } - - @Test - public void canSetBeforeInitialize() throws Exception { - HttpMessageConverters converters = new HttpMessageConverters(); - converters.setConverters(new ArrayList>()); - assertThat(converters.getConverters().size(), equalTo(0)); - } - - @Test - public void cannotSetAfterInitailzie() throws Exception { - HttpMessageConverters converters = new HttpMessageConverters(); - converters.afterPropertiesSet(); - this.thrown.expect(IllegalStateException.class); - converters.setConverters(new ArrayList>()); - } - @Test public void overrideExistingConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();