Skip to content

Commit 81c903c

Browse files
quaffwilkinsona
authored andcommitted
Add config prop for Spring Data Web's serialization mode
See gh-39797
1 parent f37e7fe commit 81c903c

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

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

Lines changed: 9 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.data.web.config.EnableSpringDataWebSupport;
3232
import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
3333
import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer;
34+
import org.springframework.data.web.config.SpringDataWebSettings;
3435
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3536

3637
/**
@@ -42,6 +43,7 @@
4243
*
4344
* @author Andy Wilkinson
4445
* @author Vedran Pavic
46+
* @author Yanming Zhou
4547
* @since 1.2.0
4648
*/
4749
@AutoConfiguration(after = RepositoryRestMvcAutoConfiguration.class)
@@ -79,4 +81,10 @@ public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
7981
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
8082
}
8183

84+
@Bean
85+
@ConditionalOnMissingBean
86+
public SpringDataWebSettings springDataWebSettings() {
87+
return new SpringDataWebSettings(this.properties.getPageable().getSerializationMode());
88+
}
89+
8290
}

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/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java

Lines changed: 44 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.
@@ -21,9 +21,13 @@
2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
2222
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2323
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
2426
import org.springframework.data.domain.PageRequest;
2527
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
2628
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
29+
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
30+
import org.springframework.data.web.config.SpringDataWebSettings;
2731

2832
import static org.assertj.core.api.Assertions.assertThat;
2933

@@ -33,6 +37,7 @@
3337
* @author Andy Wilkinson
3438
* @author Vedran Pavic
3539
* @author Stephane Nicoll
40+
* @author Yanming Zhou
3641
*/
3742
class SpringDataWebAutoConfigurationTests {
3843

@@ -53,20 +58,24 @@ void autoConfigurationBacksOffInNonWebApplicationContexts() {
5358

5459
@Test
5560
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")
61+
this.contextRunner
62+
.withPropertyValues("spring.data.web.pageable.page-parameter=p",
63+
"spring.data.web.pageable.size-parameter=s", "spring.data.web.pageable.default-page-size=10",
64+
"spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__",
65+
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.serialization-mode=VIA_DTO",
66+
"spring.data.web.pageable.one-indexed-parameters=true")
6067
.run((context) -> {
6168
PageableHandlerMethodArgumentResolver argumentResolver = context
6269
.getBean(PageableHandlerMethodArgumentResolver.class);
70+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
6371
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName", "p");
6472
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName", "s");
6573
assertThat(argumentResolver).hasFieldOrPropertyWithValue("oneIndexedParameters", true);
6674
assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix", "abc");
6775
assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter", "__");
6876
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable", PageRequest.of(0, 10));
6977
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", 100);
78+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
7079
});
7180
}
7281

@@ -76,6 +85,7 @@ void defaultPageable() {
7685
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable();
7786
PageableHandlerMethodArgumentResolver argumentResolver = context
7887
.getBean(PageableHandlerMethodArgumentResolver.class);
88+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
7989
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName",
8090
properties.getPageParameter());
8191
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName",
@@ -88,6 +98,7 @@ void defaultPageable() {
8898
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable",
8999
PageRequest.of(0, properties.getDefaultPageSize()));
90100
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", properties.getMaxPageSize());
101+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(properties.getSerializationMode());
91102
});
92103
}
93104

@@ -100,4 +111,32 @@ void customizeSort() {
100111
});
101112
}
102113

114+
@Test
115+
void customizePageSerializationModeViaConfigProps() {
116+
this.contextRunner.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO").run((context) -> {
117+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
118+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
119+
});
120+
}
121+
122+
@Test
123+
void customizePageSerializationModeViaCustomBean() {
124+
this.contextRunner.withUserConfiguration(AppConfiguration.class)
125+
.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO")
126+
.run((context) -> {
127+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
128+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.DIRECT);
129+
});
130+
}
131+
132+
@Configuration
133+
static class AppConfiguration {
134+
135+
@Bean
136+
SpringDataWebSettings springDataWebSettings() {
137+
return new SpringDataWebSettings(PageSerializationMode.DIRECT);
138+
}
139+
140+
}
141+
103142
}

0 commit comments

Comments
 (0)