Commit 261b3afc authored by Andy Wilkinson's avatar Andy Wilkinson

Apply spring.jackson.* config to Spring Data REST object mappers

Closes gh-1698
parent 886bbc3b
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.data.rest; package org.springframework.boot.autoconfigure.data.rest;
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.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
...@@ -25,6 +26,9 @@ import org.springframework.context.annotation.Bean; ...@@ -25,6 +26,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
...@@ -50,6 +54,9 @@ public class RepositoryRestMvcAutoConfiguration { ...@@ -50,6 +54,9 @@ public class RepositoryRestMvcAutoConfiguration {
static class RepositoryRestMvcBootConfiguration extends static class RepositoryRestMvcBootConfiguration extends
RepositoryRestMvcConfiguration { RepositoryRestMvcConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean @Bean
@ConfigurationProperties(prefix = "spring.data.rest") @ConfigurationProperties(prefix = "spring.data.rest")
@Override @Override
...@@ -57,5 +64,12 @@ public class RepositoryRestMvcAutoConfiguration { ...@@ -57,5 +64,12 @@ public class RepositoryRestMvcAutoConfiguration {
return super.config(); return super.config();
} }
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
}
} }
} }
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.data.rest; package org.springframework.boot.autoconfigure.data.rest;
import java.net.URI; import java.net.URI;
import java.util.Date;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
...@@ -27,15 +28,20 @@ import org.springframework.boot.autoconfigure.data.jpa.city.City; ...@@ -27,15 +28,20 @@ import org.springframework.boot.autoconfigure.data.jpa.city.City;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.BaseUri; import org.springframework.data.rest.webmvc.BaseUri;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -43,6 +49,7 @@ import static org.junit.Assert.assertNotNull; ...@@ -43,6 +49,7 @@ import static org.junit.Assert.assertNotNull;
* Tests for {@link RepositoryRestMvcAutoConfiguration}. * Tests for {@link RepositoryRestMvcAutoConfiguration}.
* *
* @author Rob Winch * @author Rob Winch
* @author Andy Wilkinson
*/ */
public class RepositoryRestMvcAutoConfigurationTests { public class RepositoryRestMvcAutoConfigurationTests {
...@@ -85,6 +92,23 @@ public class RepositoryRestMvcAutoConfigurationTests { ...@@ -85,6 +92,23 @@ public class RepositoryRestMvcAutoConfigurationTests {
} }
@Test
public void objectMappersAreConfiguredUsingObjectMapperBuilder()
throws JsonProcessingException {
load(TestConfigurationWithObjectMapperBuilder.class);
assertThatDateIsFormattedCorrectly("halObjectMapper");
assertThatDateIsFormattedCorrectly("objectMapper");
}
public void assertThatDateIsFormattedCorrectly(String beanName)
throws JsonProcessingException {
ObjectMapper objectMapper = this.context.getBean(beanName, ObjectMapper.class);
assertEquals("\"2014-10\"",
objectMapper.writeValueAsString(new Date(1413387983267L)));
}
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setServletContext(new MockServletContext()); applicationContext.setServletContext(new MockServletContext());
...@@ -110,4 +134,17 @@ public class RepositoryRestMvcAutoConfigurationTests { ...@@ -110,4 +134,17 @@ public class RepositoryRestMvcAutoConfigurationTests {
} }
@Configuration
@TestAutoConfigurationPackage(City.class)
@EnableWebMvc
static class TestConfigurationWithObjectMapperBuilder {
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder objectMapperBuilder = new Jackson2ObjectMapperBuilder();
objectMapperBuilder.simpleDateFormat("yyyy-MM");
return objectMapperBuilder;
}
}
} }
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