Skip to content

Commit 261b3af

Browse files
committed
Apply spring.jackson.* config to Spring Data REST object mappers
Closes spring-projectsgh-1698
1 parent 886bbc3 commit 261b3af

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.data.rest;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2021
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -25,6 +26,9 @@
2526
import org.springframework.context.annotation.Configuration;
2627
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2728
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
29+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
30+
31+
import com.fasterxml.jackson.databind.ObjectMapper;
2832

2933
/**
3034
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
@@ -50,12 +54,22 @@ public class RepositoryRestMvcAutoConfiguration {
5054
static class RepositoryRestMvcBootConfiguration extends
5155
RepositoryRestMvcConfiguration {
5256

57+
@Autowired(required = false)
58+
private Jackson2ObjectMapperBuilder objectMapperBuilder;
59+
5360
@Bean
5461
@ConfigurationProperties(prefix = "spring.data.rest")
5562
@Override
5663
public RepositoryRestConfiguration config() {
5764
return super.config();
5865
}
5966

67+
@Override
68+
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
69+
if (this.objectMapperBuilder != null) {
70+
this.objectMapperBuilder.configure(objectMapper);
71+
}
72+
}
73+
6074
}
6175
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.data.rest;
1818

1919
import java.net.URI;
20+
import java.util.Date;
2021

2122
import org.junit.After;
2223
import org.junit.Test;
@@ -27,22 +28,28 @@
2728
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
2829
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
2930
import org.springframework.boot.test.EnvironmentTestUtils;
31+
import org.springframework.context.annotation.Bean;
3032
import org.springframework.context.annotation.Configuration;
3133
import org.springframework.context.annotation.Import;
3234
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
3335
import org.springframework.data.rest.webmvc.BaseUri;
3436
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
37+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
3538
import org.springframework.mock.web.MockServletContext;
3639
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3740
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3841

42+
import com.fasterxml.jackson.core.JsonProcessingException;
43+
import com.fasterxml.jackson.databind.ObjectMapper;
44+
3945
import static org.junit.Assert.assertEquals;
4046
import static org.junit.Assert.assertNotNull;
4147

4248
/**
4349
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
4450
*
4551
* @author Rob Winch
52+
* @author Andy Wilkinson
4653
*/
4754
public class RepositoryRestMvcAutoConfigurationTests {
4855

@@ -85,6 +92,23 @@ public void backOffWithCustomConfiguration() {
8592

8693
}
8794

95+
@Test
96+
public void objectMappersAreConfiguredUsingObjectMapperBuilder()
97+
throws JsonProcessingException {
98+
load(TestConfigurationWithObjectMapperBuilder.class);
99+
100+
assertThatDateIsFormattedCorrectly("halObjectMapper");
101+
assertThatDateIsFormattedCorrectly("objectMapper");
102+
}
103+
104+
public void assertThatDateIsFormattedCorrectly(String beanName)
105+
throws JsonProcessingException {
106+
ObjectMapper objectMapper = this.context.getBean(beanName, ObjectMapper.class);
107+
108+
assertEquals("\"2014-10\"",
109+
objectMapper.writeValueAsString(new Date(1413387983267L)));
110+
}
111+
88112
private void load(Class<?> config, String... environment) {
89113
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
90114
applicationContext.setServletContext(new MockServletContext());
@@ -110,4 +134,17 @@ protected static class TestConfigurationWithRestMvcConfig {
110134

111135
}
112136

137+
@Configuration
138+
@TestAutoConfigurationPackage(City.class)
139+
@EnableWebMvc
140+
static class TestConfigurationWithObjectMapperBuilder {
141+
142+
@Bean
143+
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
144+
Jackson2ObjectMapperBuilder objectMapperBuilder = new Jackson2ObjectMapperBuilder();
145+
objectMapperBuilder.simpleDateFormat("yyyy-MM");
146+
return objectMapperBuilder;
147+
}
148+
}
149+
113150
}

0 commit comments

Comments
 (0)