Add JodaModule bean if detected on classpath

Fixes gh-146
pull/172/head
Dave Syer 11 years ago
parent a79e3613d3
commit 3789424f22

@ -26,6 +26,11 @@
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>commons-dbcp</groupId> <groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId> <artifactId>commons-dbcp</artifactId>

@ -23,11 +23,11 @@ import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.joda.time.DateTime;
import org.springframework.beans.factory.BeanFactoryUtils; 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.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -38,6 +38,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s. * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s.
@ -60,7 +61,17 @@ public class HttpMessageConvertersAutoConfiguration {
} }
@Configuration @Configuration
@ConditionalOnBean(ObjectMapper.class) @ConditionalOnClass({ JodaModule.class, DateTime.class })
protected static class JodaModuleConfiguration {
@Bean
public JodaModule jodaModule() {
return new JodaModule();
}
}
@Configuration
@ConditionalOnClass(ObjectMapper.class) @ConditionalOnClass(ObjectMapper.class)
protected static class ObjectMappers { protected static class ObjectMappers {

@ -37,6 +37,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -72,6 +73,14 @@ public class HttpMessageConvertersAutoConfigurationTests {
assertTrue(converters.getConverters().contains(converter)); assertTrue(converters.getConverters().contains(converter));
} }
@Test
public void defaultJacksonModules() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(HttpMessageConvertersAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean("jodaModule", Module.class));
}
@Test @Test
public void customJacksonModules() throws Exception { public void customJacksonModules() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
@ -82,7 +91,8 @@ public class HttpMessageConvertersAutoConfigurationTests {
@SuppressWarnings({ "unchecked", "unused" }) @SuppressWarnings({ "unchecked", "unused" })
ObjectMapper result = verify(mapper).registerModules( ObjectMapper result = verify(mapper).registerModules(
(Iterable<Module>) argThat(hasItem(this.context.getBean(Module.class)))); (Iterable<Module>) argThat(hasItem(this.context.getBean("jacksonModule",
Module.class))));
} }
@Test @Test

Loading…
Cancel
Save