Skip to content

Commit 9db35be

Browse files
committed
Add configuration key "spring.data.web.pageable.serialization-mode" defaults to DIRECT
see spring-projects/spring-data-commons@5dd7b32
1 parent 70769d9 commit 9db35be

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,11 +26,13 @@
2626
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable;
2727
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2828
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Primary;
2930
import org.springframework.data.domain.PageRequest;
3031
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
3132
import org.springframework.data.web.config.EnableSpringDataWebSupport;
3233
import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
3334
import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer;
35+
import org.springframework.data.web.config.SpringDataWebSettings;
3436
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3537

3638
/**
@@ -42,6 +44,7 @@
4244
*
4345
* @author Andy Wilkinson
4446
* @author Vedran Pavic
47+
* @author Yanming Zhou
4548
* @since 1.2.0
4649
*/
4750
@AutoConfiguration(after = RepositoryRestMvcAutoConfiguration.class)
@@ -79,4 +82,10 @@ public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
7982
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
8083
}
8184

85+
@Primary // override bean created by @EnableSpringDataWebSupport
86+
@Bean
87+
public SpringDataWebSettings springDataWebSettings() {
88+
return new SpringDataWebSettings(this.properties.getPageable().getSerializationMode());
89+
}
90+
8291
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebProperties.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@
1717
package org.springframework.boot.autoconfigure.data.web;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
2021

2122
/**
2223
* Configuration properties for Spring Data Web.
2324
*
2425
* @author Vedran Pavic
26+
* @author Yanming Zhou
2527
* @since 2.0.0
2628
*/
2729
@ConfigurationProperties("spring.data.web")
@@ -81,6 +83,11 @@ public static class Pageable {
8183
*/
8284
private int maxPageSize = 2000;
8385

86+
/**
87+
* Configures how to render spring data Pageable instances.
88+
*/
89+
private PageSerializationMode serializationMode = PageSerializationMode.DIRECT;
90+
8491
public String getPageParameter() {
8592
return this.pageParameter;
8693
}
@@ -137,6 +144,14 @@ public void setMaxPageSize(int maxPageSize) {
137144
this.maxPageSize = maxPageSize;
138145
}
139146

147+
public PageSerializationMode getSerializationMode() {
148+
return this.serializationMode;
149+
}
150+
151+
public void setSerializationMode(PageSerializationMode serializationMode) {
152+
this.serializationMode = serializationMode;
153+
}
154+
140155
}
141156

142157
/**

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,12 @@
11531153
"name": "spring.data.rest.detection-strategy",
11541154
"defaultValue": "default"
11551155
},
1156+
{
1157+
"name": "spring.data.web.pageable.serialization-mode",
1158+
"type": "org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode",
1159+
"description": "Configures how to render spring data Pageable instances.",
1160+
"defaultValue": "DIRECT"
1161+
},
11561162
{
11571163
"name" : "spring.datasource.continue-on-error",
11581164
"type" : "java.lang.Boolean",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import org.springframework.data.domain.PageRequest;
2525
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
2626
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
27+
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
28+
import org.springframework.data.web.config.SpringDataWebSettings;
2729

2830
import static org.assertj.core.api.Assertions.assertThat;
2931

@@ -33,6 +35,7 @@
3335
* @author Andy Wilkinson
3436
* @author Vedran Pavic
3537
* @author Stephane Nicoll
38+
* @author Yanming Zhou
3639
*/
3740
class SpringDataWebAutoConfigurationTests {
3841

@@ -53,20 +56,24 @@ void autoConfigurationBacksOffInNonWebApplicationContexts() {
5356

5457
@Test
5558
void customizePageable() {
56-
this.contextRunner.withPropertyValues("spring.data.web.pageable.page-parameter=p",
57-
"spring.data.web.pageable.size-parameter=s", "spring.data.web.pageable.default-page-size=10",
58-
"spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__",
59-
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.one-indexed-parameters=true")
59+
this.contextRunner
60+
.withPropertyValues("spring.data.web.pageable.page-parameter=p",
61+
"spring.data.web.pageable.size-parameter=s", "spring.data.web.pageable.default-page-size=10",
62+
"spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__",
63+
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.serialization-mode=VIA_DTO",
64+
"spring.data.web.pageable.one-indexed-parameters=true")
6065
.run((context) -> {
6166
PageableHandlerMethodArgumentResolver argumentResolver = context
6267
.getBean(PageableHandlerMethodArgumentResolver.class);
68+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
6369
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName", "p");
6470
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName", "s");
6571
assertThat(argumentResolver).hasFieldOrPropertyWithValue("oneIndexedParameters", true);
6672
assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix", "abc");
6773
assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter", "__");
6874
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable", PageRequest.of(0, 10));
6975
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", 100);
76+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
7077
});
7178
}
7279

@@ -76,6 +83,7 @@ void defaultPageable() {
7683
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable();
7784
PageableHandlerMethodArgumentResolver argumentResolver = context
7885
.getBean(PageableHandlerMethodArgumentResolver.class);
86+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
7987
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName",
8088
properties.getPageParameter());
8189
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName",
@@ -88,6 +96,7 @@ void defaultPageable() {
8896
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable",
8997
PageRequest.of(0, properties.getDefaultPageSize()));
9098
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", properties.getMaxPageSize());
99+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(properties.getSerializationMode());
91100
});
92101
}
93102

@@ -100,4 +109,12 @@ void customizeSort() {
100109
});
101110
}
102111

112+
@Test
113+
void customizePageSerializationMode() {
114+
this.contextRunner.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO").run((context) -> {
115+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
116+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
117+
});
118+
}
119+
103120
}

spring-boot-project/spring-boot-dependencies/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ bom {
17951795
releaseNotes("https://github.com/spring-projects/spring-batch/releases/tag/v{version}")
17961796
}
17971797
}
1798-
library("Spring Data Bom", "2023.1.3") {
1798+
library("Spring Data Bom", "2024.0.0-SNAPSHOT") {
17991799
considerSnapshots()
18001800
calendarName = "Spring Data Release"
18011801
group("org.springframework.data") {

0 commit comments

Comments
 (0)