diff --git a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java index de6f4e44b..32635ef3a 100644 --- a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java +++ b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java @@ -69,6 +69,7 @@ import org.springframework.util.ClassUtils; * @see SpringDataWebConfiguration * @see HateoasAwareSpringDataWebConfiguration * @author Oliver Gierke + * @author Yanming Zhou */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE }) @@ -170,6 +171,7 @@ public @interface EnableSpringDataWebSupport { * {@link EnableSpringDataWebSupport}. * * @author Oliver Drotbohm + * @author Yanming Zhou * @soundtrack Norah Jones - Chasing Pirates * @since 3.3 */ @@ -190,8 +192,14 @@ public @interface EnableSpringDataWebSupport { return; } + Object pageSerializationMode = attributes.get("pageSerializationMode"); + + if (pageSerializationMode == PageSerializationMode.DIRECT) { + return; + } + AbstractBeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(SpringDataWebSettings.class) - .addConstructorArgValue(attributes.get("pageSerializationMode")) + .addConstructorArgValue(pageSerializationMode) .getBeanDefinition(); String beanName = importBeanNameGenerator.generateBeanName(definition, registry); diff --git a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java index a0fb2bae2..75ca7709d 100755 --- a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java +++ b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java @@ -24,6 +24,7 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -67,7 +68,7 @@ class EnableSpringDataWebSupportIntegrationTests { @Configuration @EnableWebMvc - @EnableSpringDataWebSupport + @EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO) static class SampleConfig { @Bean @@ -304,6 +305,10 @@ class EnableSpringDataWebSupportIntegrationTests { void usesDirectPageSerializationMode() throws Exception { var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class); + + // SpringDataWebSettings shouldn't be registered if pageSerializationMode is default + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> applicationContext.getBean(SpringDataWebSettings.class)); + var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build(); mvc.perform(post("/page"))//