mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Remove initialization concerns fom HttpMessageConverters
This commit is contained in:
parent
513c6a1de2
commit
bdcb94a139
@ -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<HttpMessageConverter<?>>,
|
||||
InitializingBean {
|
||||
public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> {
|
||||
|
||||
private List<HttpMessageConverter<?>> converters;
|
||||
|
||||
private boolean initialized;
|
||||
|
||||
/**
|
||||
* Create a new {@link HttpMessageConverters} instance with the specified additional
|
||||
* converters.
|
||||
@ -77,18 +71,19 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>,
|
||||
* converter manipulation.
|
||||
*/
|
||||
public HttpMessageConverters(Collection<HttpMessageConverter<?>> additionalConverters) {
|
||||
this.converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
List<HttpMessageConverter<?>> 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<HttpMessageConverter<?>> getDefaultConverters() {
|
||||
@ -139,19 +134,4 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>,
|
||||
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<HttpMessageConverter<?>> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<HttpMessageConverter<?>>());
|
||||
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<HttpMessageConverter<?>>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overrideExistingConverter() {
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
|
Loading…
Reference in New Issue
Block a user