|
20 | 20 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
21 | 21 |
|
22 | 22 | import java.util.Arrays;
|
| 23 | +import java.util.List; |
23 | 24 |
|
| 25 | +import com.fasterxml.jackson.databind.Module; |
24 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
25 | 28 | import org.springframework.context.ApplicationContext;
|
26 | 29 | import org.springframework.context.annotation.Bean;
|
27 | 30 | import org.springframework.context.annotation.Configuration;
|
| 31 | +import org.springframework.context.annotation.Primary; |
28 | 32 | import org.springframework.core.convert.ConversionService;
|
29 | 33 | import org.springframework.data.classloadersupport.HidingClassLoader;
|
30 | 34 | import org.springframework.data.geo.Distance;
|
|
40 | 44 | import org.springframework.data.web.WebTestUtils;
|
41 | 45 | import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule;
|
42 | 46 | import org.springframework.hateoas.Link;
|
| 47 | +import org.springframework.http.converter.HttpMessageConverter; |
| 48 | +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
| 49 | +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
43 | 50 | import org.springframework.test.util.ReflectionTestUtils;
|
44 | 51 | import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
45 | 52 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
| 53 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; |
46 | 54 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
47 | 55 | import org.springframework.web.util.UriComponentsBuilder;
|
48 | 56 |
|
@@ -114,6 +122,43 @@ SimpleEntityPathResolver entityPathResolver() {
|
114 | 122 | }
|
115 | 123 | }
|
116 | 124 |
|
| 125 | + @Configuration |
| 126 | + static class PageSampleConfig extends WebMvcConfigurationSupport { |
| 127 | + |
| 128 | + @Autowired |
| 129 | + private List<Module> modules; |
| 130 | + |
| 131 | + @Bean |
| 132 | + PageSampleController controller() { |
| 133 | + return new PageSampleController(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
| 138 | + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().modules(modules); |
| 139 | + converters.add(0, new MappingJackson2HttpMessageConverter(builder.build())); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + @EnableSpringDataWebSupport |
| 145 | + static class PageSampleConfigWithDirect extends PageSampleConfig { |
| 146 | + } |
| 147 | + |
| 148 | + @EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO) |
| 149 | + static class PageSampleConfigWithViaDto extends PageSampleConfig { |
| 150 | + } |
| 151 | + |
| 152 | + @EnableSpringDataWebSupport |
| 153 | + static class PageSampleConfigWithSpringDataWebSettings extends PageSampleConfig { |
| 154 | + |
| 155 | + @Primary |
| 156 | + @Bean |
| 157 | + SpringDataWebSettings SpringDataWebSettings() { |
| 158 | + return new SpringDataWebSettings(EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO); |
| 159 | + } |
| 160 | + } |
| 161 | + |
117 | 162 | @Test // DATACMNS-330
|
118 | 163 | void registersBasicBeanDefinitions() throws Exception {
|
119 | 164 |
|
@@ -273,6 +318,39 @@ void registersSpringDataWebSettingsBean() {
|
273 | 318 | });
|
274 | 319 | }
|
275 | 320 |
|
| 321 | + @Test // GH-3024 |
| 322 | + void usesDirectPageSerializationMode() throws Exception { |
| 323 | + |
| 324 | + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class); |
| 325 | + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); |
| 326 | + |
| 327 | + mvc.perform(post("/page")).// |
| 328 | + andExpect(status().isOk()).// |
| 329 | + andExpect(jsonPath("$.pageable").exists()); |
| 330 | + } |
| 331 | + |
| 332 | + @Test // GH-3024 |
| 333 | + void usesViaDtoPageSerializationMode() throws Exception { |
| 334 | + |
| 335 | + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithViaDto.class); |
| 336 | + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); |
| 337 | + |
| 338 | + mvc.perform(post("/page")).// |
| 339 | + andExpect(status().isOk()).// |
| 340 | + andExpect(jsonPath("$.page").exists()); |
| 341 | + } |
| 342 | + |
| 343 | + @Test // GH-3024 |
| 344 | + void overridesPageSerializationModeByCustomizingSpringDataWebSettings() throws Exception { |
| 345 | + |
| 346 | + var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithSpringDataWebSettings.class); |
| 347 | + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); |
| 348 | + |
| 349 | + mvc.perform(post("/page")).// |
| 350 | + andExpect(status().isOk()).// |
| 351 | + andExpect(jsonPath("$.page").exists()); |
| 352 | + } |
| 353 | + |
276 | 354 | private static void assertResolversRegistered(ApplicationContext context, Class<?>... resolverTypes) {
|
277 | 355 |
|
278 | 356 | var adapter = context.getBean(RequestMappingHandlerAdapter.class);
|
|
0 commit comments