diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java index 584ec7010da..1f3c63cfcb3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,11 +26,13 @@ import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoCon import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; import org.springframework.data.domain.PageRequest; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.data.web.config.EnableSpringDataWebSupport; import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer; import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer; +import org.springframework.data.web.config.SpringDataWebSettings; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -42,6 +44,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * * @author Andy Wilkinson * @author Vedran Pavic + * @author Yanming Zhou * @since 1.2.0 */ @AutoConfiguration(after = RepositoryRestMvcAutoConfiguration.class) @@ -79,4 +82,10 @@ public class SpringDataWebAutoConfiguration { return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter()); } + @Primary // override bean created by @EnableSpringDataWebSupport + @Bean + public SpringDataWebSettings springDataWebSettings() { + return new SpringDataWebSettings(this.properties.getPageable().getSerializationMode()); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java index a736f404036..529ebd2c67d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +17,13 @@ package org.springframework.boot.autoconfigure.data.web; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode; /** * Configuration properties for Spring Data Web. * * @author Vedran Pavic + * @author Yanming Zhou * @since 2.0.0 */ @ConfigurationProperties("spring.data.web") @@ -81,6 +83,11 @@ public class SpringDataWebProperties { */ private int maxPageSize = 2000; + /** + * Configures how to render spring data Pageable instances. + */ + private PageSerializationMode serializationMode = PageSerializationMode.DIRECT; + public String getPageParameter() { return this.pageParameter; } @@ -137,6 +144,14 @@ public class SpringDataWebProperties { this.maxPageSize = maxPageSize; } + public PageSerializationMode getSerializationMode() { + return this.serializationMode; + } + + public void setSerializationMode(PageSerializationMode serializationMode) { + this.serializationMode = serializationMode; + } + } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 08c1e78c146..9458806208b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1157,6 +1157,12 @@ "name": "spring.data.rest.detection-strategy", "defaultValue": "default" }, + { + "name": "spring.data.web.pageable.serialization-mode", + "type": "org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode", + "description": "Configures how to render spring data Pageable instances.", + "defaultValue": "DIRECT" + }, { "name" : "spring.datasource.continue-on-error", "type" : "java.lang.Boolean", diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java index 67cf7109ca3..655596eb92e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,8 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.data.domain.PageRequest; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.data.web.SortHandlerMethodArgumentResolver; +import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode; +import org.springframework.data.web.config.SpringDataWebSettings; import static org.assertj.core.api.Assertions.assertThat; @@ -33,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Andy Wilkinson * @author Vedran Pavic * @author Stephane Nicoll + * @author Yanming Zhou */ class SpringDataWebAutoConfigurationTests { @@ -53,13 +56,16 @@ class SpringDataWebAutoConfigurationTests { @Test void customizePageable() { - this.contextRunner.withPropertyValues("spring.data.web.pageable.page-parameter=p", - "spring.data.web.pageable.size-parameter=s", "spring.data.web.pageable.default-page-size=10", - "spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__", - "spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.one-indexed-parameters=true") + this.contextRunner + .withPropertyValues("spring.data.web.pageable.page-parameter=p", + "spring.data.web.pageable.size-parameter=s", "spring.data.web.pageable.default-page-size=10", + "spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__", + "spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.serialization-mode=VIA_DTO", + "spring.data.web.pageable.one-indexed-parameters=true") .run((context) -> { PageableHandlerMethodArgumentResolver argumentResolver = context .getBean(PageableHandlerMethodArgumentResolver.class); + SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class); assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName", "p"); assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName", "s"); assertThat(argumentResolver).hasFieldOrPropertyWithValue("oneIndexedParameters", true); @@ -67,6 +73,7 @@ class SpringDataWebAutoConfigurationTests { assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter", "__"); assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable", PageRequest.of(0, 10)); assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", 100); + assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO); }); } @@ -76,6 +83,7 @@ class SpringDataWebAutoConfigurationTests { SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable(); PageableHandlerMethodArgumentResolver argumentResolver = context .getBean(PageableHandlerMethodArgumentResolver.class); + SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class); assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName", properties.getPageParameter()); assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName", @@ -88,6 +96,7 @@ class SpringDataWebAutoConfigurationTests { assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable", PageRequest.of(0, properties.getDefaultPageSize())); assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", properties.getMaxPageSize()); + assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(properties.getSerializationMode()); }); } @@ -100,4 +109,12 @@ class SpringDataWebAutoConfigurationTests { }); } + @Test + void customizePageSerializationMode() { + this.contextRunner.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO").run((context) -> { + SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class); + assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO); + }); + } + }