Commit b7200214 authored by Dave Syer's avatar Dave Syer

Split MessageConverters auto config out into separate class

parent 9ffa6bce
...@@ -30,6 +30,7 @@ import org.springframework.boot.actuate.properties.ManagementServerProperties; ...@@ -30,6 +30,7 @@ import org.springframework.boot.actuate.properties.ManagementServerProperties;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
...@@ -69,6 +70,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -69,6 +70,7 @@ public class EndpointWebMvcAutoConfigurationTests {
this.applicationContext.register(RootConfig.class, this.applicationContext.register(RootConfig.class,
PropertyPlaceholderAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class); EndpointWebMvcAutoConfiguration.class);
...@@ -86,6 +88,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -86,6 +88,7 @@ public class EndpointWebMvcAutoConfigurationTests {
this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, this.applicationContext.register(RootConfig.class, DifferentPortConfig.class,
PropertyPlaceholderAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
...@@ -103,6 +106,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -103,6 +106,7 @@ public class EndpointWebMvcAutoConfigurationTests {
this.applicationContext.register(RootConfig.class, DisableConfig.class, this.applicationContext.register(RootConfig.class, DisableConfig.class,
PropertyPlaceholderAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class); EndpointWebMvcAutoConfiguration.class);
...@@ -124,6 +128,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -124,6 +128,7 @@ public class EndpointWebMvcAutoConfigurationTests {
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
this.applicationContext.refresh(); this.applicationContext.refresh();
...@@ -143,6 +148,7 @@ public class EndpointWebMvcAutoConfigurationTests { ...@@ -143,6 +148,7 @@ public class EndpointWebMvcAutoConfigurationTests {
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class); EndpointWebMvcAutoConfiguration.class);
this.applicationContext.refresh(); this.applicationContext.refresh();
......
...@@ -23,6 +23,7 @@ import org.jolokia.http.AgentServlet; ...@@ -23,6 +23,7 @@ import org.jolokia.http.AgentServlet;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.TestUtils; import org.springframework.boot.TestUtils;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
...@@ -59,7 +60,7 @@ public class JolokiaAutoConfigurationTests { ...@@ -59,7 +60,7 @@ public class JolokiaAutoConfigurationTests {
public void agentServletRegisteredWithAppContext() throws Exception { public void agentServletRegisteredWithAppContext() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
JolokiaAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(AgentServlet.class).length); assertEquals(1, this.context.getBeanNamesForType(AgentServlet.class).length);
} }
...@@ -69,7 +70,7 @@ public class JolokiaAutoConfigurationTests { ...@@ -69,7 +70,7 @@ public class JolokiaAutoConfigurationTests {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
TestUtils.addEnviroment(this.context, "endpoints.jolokia.enabled:false"); TestUtils.addEnviroment(this.context, "endpoints.jolokia.enabled:false");
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
JolokiaAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(0, this.context.getBeanNamesForType(AgentServlet.class).length); assertEquals(0, this.context.getBeanNamesForType(AgentServlet.class).length);
} }
...@@ -78,7 +79,7 @@ public class JolokiaAutoConfigurationTests { ...@@ -78,7 +79,7 @@ public class JolokiaAutoConfigurationTests {
public void agentServletRegisteredWithServletContainer() throws Exception { public void agentServletRegisteredWithServletContainer() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
JolokiaAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class, JolokiaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Servlet servlet = null; Servlet servlet = null;
......
...@@ -22,6 +22,7 @@ import org.springframework.boot.TestUtils; ...@@ -22,6 +22,7 @@ import org.springframework.boot.TestUtils;
import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer; import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.context.initializer.LoggingApplicationContextInitializer; import org.springframework.boot.context.initializer.LoggingApplicationContextInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -66,6 +67,7 @@ public class ManagementSecurityAutoConfigurationTests { ...@@ -66,6 +67,7 @@ public class ManagementSecurityAutoConfigurationTests {
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, this.context.register(SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class, ManagementSecurityAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
...@@ -82,6 +84,7 @@ public class ManagementSecurityAutoConfigurationTests { ...@@ -82,6 +84,7 @@ public class ManagementSecurityAutoConfigurationTests {
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(EndpointAutoConfiguration.class, this.context.register(EndpointAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
SecurityAutoConfiguration.class, SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class, ManagementSecurityAutoConfiguration.class,
...@@ -125,6 +128,7 @@ public class ManagementSecurityAutoConfigurationTests { ...@@ -125,6 +128,7 @@ public class ManagementSecurityAutoConfigurationTests {
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, this.context.register(SecurityAutoConfiguration.class,
ManagementSecurityAutoConfiguration.class, ManagementSecurityAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
......
/*
* Copyright 2012-2013 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
*
* http://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.autoconfigure.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Dave Syer
*/
@Configuration
@ConditionalOnClass(HttpMessageConverter.class)
public class HttpMessageConvertersAutoConfiguration {
@Autowired(required = false)
private List<HttpMessageConverter<?>> converters = Collections.emptyList();
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
this.converters);
return new HttpMessageConverters(converters);
}
@Configuration
@ConditionalOnBean(ObjectMapper.class)
@ConditionalOnClass(ObjectMapper.class)
protected static class ObjectMappers {
@Autowired
private ListableBeanFactory beanFactory;
@PostConstruct
public void init() {
Collection<ObjectMapper> mappers = BeanFactoryUtils
.beansOfTypeIncludingAncestors(this.beanFactory, ObjectMapper.class)
.values();
Collection<Module> modules = BeanFactoryUtils.beansOfTypeIncludingAncestors(
this.beanFactory, Module.class).values();
for (ObjectMapper mapper : mappers) {
mapper.registerModules(modules);
}
}
@Bean
@ConditionalOnMissingBean
@Primary
public ObjectMapper jacksonObjectMapper() {
return new ObjectMapper();
}
@Bean
@ConditionalOnMissingBean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
ObjectMapper objectMapper) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper);
return converter;
}
}
}
...@@ -17,19 +17,16 @@ ...@@ -17,19 +17,16 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -41,7 +38,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -41,7 +38,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
...@@ -52,7 +48,6 @@ import org.springframework.core.io.ResourceLoader; ...@@ -52,7 +48,6 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.format.Formatter; import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.RequestContextListener; import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.filter.HiddenHttpMethodFilter;
...@@ -69,9 +64,6 @@ import org.springframework.web.servlet.view.BeanNameViewResolver; ...@@ -69,9 +64,6 @@ import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebMvc Web MVC}. * {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebMvc Web MVC}.
* *
...@@ -111,17 +103,6 @@ public class WebMvcAutoConfiguration { ...@@ -111,17 +103,6 @@ public class WebMvcAutoConfiguration {
} }
} }
@Autowired(required = false)
private List<HttpMessageConverter<?>> converters = Collections.emptyList();
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
this.converters);
return new HttpMessageConverters(converters);
}
@Bean @Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class) @ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
public HiddenHttpMethodFilter hiddenHttpMethodFilter() { public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
...@@ -155,45 +136,6 @@ public class WebMvcAutoConfiguration { ...@@ -155,45 +136,6 @@ public class WebMvcAutoConfiguration {
converters.addAll(this.messageConverters.getMessageConverters()); converters.addAll(this.messageConverters.getMessageConverters());
} }
@Configuration
@ConditionalOnBean(ObjectMapper.class)
@ConditionalOnClass(ObjectMapper.class)
protected static class ObjectMappers {
@Autowired
private ListableBeanFactory beanFactory;
@PostConstruct
public void init() {
Collection<ObjectMapper> mappers = BeanFactoryUtils
.beansOfTypeIncludingAncestors(this.beanFactory,
ObjectMapper.class).values();
Collection<Module> modules = BeanFactoryUtils
.beansOfTypeIncludingAncestors(this.beanFactory, Module.class)
.values();
for (ObjectMapper mapper : mappers) {
mapper.registerModules(modules);
}
}
@Bean
@ConditionalOnMissingBean
@Primary
public ObjectMapper jacksonObjectMapper() {
return new ObjectMapper();
}
@Bean
@ConditionalOnMissingBean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
ObjectMapper objectMapper) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper);
return converter;
}
}
@Bean @Bean
@ConditionalOnMissingBean(InternalResourceViewResolver.class) @ConditionalOnMissingBean(InternalResourceViewResolver.class)
public InternalResourceViewResolver defaultViewResolver() { public InternalResourceViewResolver defaultViewResolver() {
......
...@@ -21,6 +21,7 @@ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfigura ...@@ -21,6 +21,7 @@ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfigura
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\ org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration
......
...@@ -29,6 +29,7 @@ import org.junit.Before; ...@@ -29,6 +29,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -180,7 +181,7 @@ public class AutoConfigurationReportLoggingInitializerTests { ...@@ -180,7 +181,7 @@ public class AutoConfigurationReportLoggingInitializerTests {
} }
@Configuration @Configuration
@Import(WebMvcAutoConfiguration.class) @Import({ WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
static class Config { static class Config {
} }
......
...@@ -16,14 +16,12 @@ ...@@ -16,14 +16,12 @@
package org.springframework.boot.autoconfigure.mobile; package org.springframework.boot.autoconfigure.mobile;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
...@@ -37,6 +35,9 @@ import org.springframework.util.ReflectionUtils; ...@@ -37,6 +35,9 @@ import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/** /**
* Tests for {@link DeviceResolverAutoConfiguration}. * Tests for {@link DeviceResolverAutoConfiguration}.
* *
...@@ -76,6 +77,7 @@ public class DeviceResolverAutoConfigurationTests { ...@@ -76,6 +77,7 @@ public class DeviceResolverAutoConfigurationTests {
public void deviceResolverHandlerInterceptorRegistered() throws Exception { public void deviceResolverHandlerInterceptorRegistered() throws Exception {
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
context.register(Config.class, WebMvcAutoConfiguration.class, context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DeviceResolverAutoConfiguration.class); DeviceResolverAutoConfiguration.class);
context.refresh(); context.refresh();
RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context
......
/*
* Copyright 2012-2013 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
*
* http://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.autoconfigure.web;
import java.io.IOException;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify;
/**
* @author Dave Syer
*/
public class HttpMessageConvertersAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void customJacksonConverter() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(JacksonConfig.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
MappingJackson2HttpMessageConverter converter = this.context
.getBean(MappingJackson2HttpMessageConverter.class);
assertEquals(this.context.getBean(ObjectMapper.class),
converter.getObjectMapper());
HttpMessageConverters converters = this.context
.getBean(HttpMessageConverters.class);
assertTrue(converters.getMessageConverters().contains(converter));
}
@Test
public void customJacksonModules() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(ModulesConfig.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
@SuppressWarnings({ "unchecked", "unused" })
ObjectMapper result = verify(mapper).registerModules(
(Iterable<Module>) argThat(hasItem(this.context.getBean(Module.class))));
}
@Test
public void doubleModuleRegistration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(DoubleModulesConfig.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
assertEquals("{\"foo\":\"bar\"}", mapper.writeValueAsString(new Foo()));
}
@Configuration
protected static class JacksonConfig {
@Bean
public MappingJackson2HttpMessageConverter jacksonMessaegConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
@Configuration
protected static class ModulesConfig {
@Bean
public Module jacksonModule() {
return new SimpleModule();
}
@Bean
@Primary
public ObjectMapper objectMapper() {
return Mockito.mock(ObjectMapper.class);
}
}
@Configuration
protected static class DoubleModulesConfig {
@Bean
public Module jacksonModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Foo.class, new JsonSerializer<Foo>() {
@Override
public void serialize(Foo value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("foo", "bar");
jgen.writeEndObject();
}
});
return module;
}
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(jacksonModule());
return mapper;
}
}
protected static class Foo {
private String name;
private Foo() {
}
static Foo create() {
return new Foo();
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
...@@ -29,17 +28,14 @@ import org.junit.After; ...@@ -29,17 +28,14 @@ import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
...@@ -51,22 +47,10 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl ...@@ -51,22 +47,10 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link WebMvcAutoConfiguration}. * Tests for {@link WebMvcAutoConfiguration}.
...@@ -93,25 +77,32 @@ public class WebMvcAutoConfigurationTests { ...@@ -93,25 +77,32 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void handerAdaptersCreated() throws Exception { public void handerAdaptersCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class); this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length); assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length);
assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class)
.getMessageConverters().isEmpty());
assertEquals(this.context.getBean(HttpMessageConverters.class)
.getMessageConverters(),
this.context.getBean(RequestMappingHandlerAdapter.class)
.getMessageConverters());
} }
@Test @Test
public void handerMappingsCreated() throws Exception { public void handerMappingsCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class); this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length);
assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class)
.getMessageConverters().isEmpty());
} }
@Test @Test
public void resourceHandlerMapping() throws Exception { public void resourceHandlerMapping() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class); this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(5)); assertThat(mappingLocations.get("/**").size(), equalTo(5));
...@@ -123,7 +114,8 @@ public class WebMvcAutoConfigurationTests { ...@@ -123,7 +114,8 @@ public class WebMvcAutoConfigurationTests {
@Test @Test
public void resourceHandlerMappingOverrideWebjars() throws Exception { public void resourceHandlerMappingOverrideWebjars() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class); this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
...@@ -135,7 +127,8 @@ public class WebMvcAutoConfigurationTests { ...@@ -135,7 +127,8 @@ public class WebMvcAutoConfigurationTests {
public void resourceHandlerMappingOverrideAll() throws Exception { public void resourceHandlerMappingOverrideAll() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(AllResources.class, Config.class, this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class); WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(1)); assertThat(mappingLocations.get("/**").size(), equalTo(1));
...@@ -143,46 +136,6 @@ public class WebMvcAutoConfigurationTests { ...@@ -143,46 +136,6 @@ public class WebMvcAutoConfigurationTests {
equalTo((Resource) new ClassPathResource("/foo/"))); equalTo((Resource) new ClassPathResource("/foo/")));
} }
@Test
public void customJacksonConverter() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(JacksonConfig.class, Config.class,
WebMvcAutoConfiguration.class);
this.context.refresh();
MappingJackson2HttpMessageConverter converter = this.context
.getBean(MappingJackson2HttpMessageConverter.class);
assertEquals(this.context.getBean(ObjectMapper.class),
converter.getObjectMapper());
HttpMessageConverters converters = this.context.getBean(HttpMessageConverters.class);
assertTrue(converters.getMessageConverters().contains(converter));
assertEquals(converters.getMessageConverters(),
this.context.getBean(RequestMappingHandlerAdapter.class)
.getMessageConverters());
}
@Test
public void customJacksonModules() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(ModulesConfig.class, Config.class,
WebMvcAutoConfiguration.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
@SuppressWarnings({ "unchecked", "unused" })
ObjectMapper result = verify(mapper).registerModules(
(Iterable<Module>) argThat(hasItem(this.context.getBean(Module.class))));
}
@Test
public void doubleModuleRegistration() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(DoubleModulesConfig.class, Config.class,
WebMvcAutoConfiguration.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
assertEquals("{\"foo\":\"bar\"}", mapper.writeValueAsString(new Foo()));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected Map<String, List<Resource>> getMappingLocations() protected Map<String, List<Resource>> getMappingLocations()
throws IllegalAccessException { throws IllegalAccessException {
...@@ -240,91 +193,6 @@ public class WebMvcAutoConfigurationTests { ...@@ -240,91 +193,6 @@ public class WebMvcAutoConfigurationTests {
} }
@Configuration
protected static class ModulesConfig {
@Bean
public Module jacksonModule() {
return new SimpleModule();
}
@Bean
@Primary
public ObjectMapper objectMapper() {
return Mockito.mock(ObjectMapper.class);
}
}
@Configuration
protected static class DoubleModulesConfig {
@Bean
public Module jacksonModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(Foo.class, new JsonSerializer<Foo>() {
@Override
public void serialize(Foo value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("foo", "bar");
jgen.writeEndObject();
}
});
return module;
}
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(jacksonModule());
return mapper;
}
}
protected static class Foo {
private String name;
private Foo() {
}
static Foo create() {
return new Foo();
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
@Configuration
protected static class JacksonConfig {
@Bean
public MappingJackson2HttpMessageConverter jacksonMessaegConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
@Configuration @Configuration
protected static class Config { protected static class Config {
......
...@@ -29,6 +29,7 @@ import org.springframework.boot.SpringApplication; ...@@ -29,6 +29,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.sample.tomcat.service.HelloWorldService; import org.springframework.boot.sample.tomcat.service.HelloWorldService;
import org.springframework.boot.sample.tomcat.web.SampleController; import org.springframework.boot.sample.tomcat.web.SampleController;
...@@ -55,7 +56,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests { ...@@ -55,7 +56,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests {
@Configuration @Configuration
@Import({ EmbeddedServletContainerAutoConfiguration.class, @Import({ EmbeddedServletContainerAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class }) PropertyPlaceholderAutoConfiguration.class })
@ComponentScan(basePackageClasses = { SampleController.class, HelloWorldService.class }) @ComponentScan(basePackageClasses = { SampleController.class, HelloWorldService.class })
public static class NonAutoConfigurationSampleTomcatApplication { public static class NonAutoConfigurationSampleTomcatApplication {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment