17
17
package org .springframework .boot .autoconfigure .data .rest ;
18
18
19
19
import java .net .URI ;
20
+ import java .util .Date ;
20
21
21
22
import org .junit .After ;
22
23
import org .junit .Test ;
27
28
import org .springframework .boot .autoconfigure .jdbc .EmbeddedDataSourceConfiguration ;
28
29
import org .springframework .boot .autoconfigure .orm .jpa .HibernateJpaAutoConfiguration ;
29
30
import org .springframework .boot .test .EnvironmentTestUtils ;
31
+ import org .springframework .context .annotation .Bean ;
30
32
import org .springframework .context .annotation .Configuration ;
31
33
import org .springframework .context .annotation .Import ;
32
34
import org .springframework .data .rest .core .config .RepositoryRestConfiguration ;
33
35
import org .springframework .data .rest .webmvc .BaseUri ;
34
36
import org .springframework .data .rest .webmvc .config .RepositoryRestMvcConfiguration ;
37
+ import org .springframework .http .converter .json .Jackson2ObjectMapperBuilder ;
35
38
import org .springframework .mock .web .MockServletContext ;
36
39
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
37
40
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
38
41
42
+ import com .fasterxml .jackson .core .JsonProcessingException ;
43
+ import com .fasterxml .jackson .databind .ObjectMapper ;
44
+
39
45
import static org .junit .Assert .assertEquals ;
40
46
import static org .junit .Assert .assertNotNull ;
41
47
42
48
/**
43
49
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
44
50
*
45
51
* @author Rob Winch
52
+ * @author Andy Wilkinson
46
53
*/
47
54
public class RepositoryRestMvcAutoConfigurationTests {
48
55
@@ -85,6 +92,23 @@ public void backOffWithCustomConfiguration() {
85
92
86
93
}
87
94
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
+
88
112
private void load (Class <?> config , String ... environment ) {
89
113
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext ();
90
114
applicationContext .setServletContext (new MockServletContext ());
@@ -110,4 +134,17 @@ protected static class TestConfigurationWithRestMvcConfig {
110
134
111
135
}
112
136
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
+
113
150
}
0 commit comments