Skip to content

Commit d23528a

Browse files
committed
Merge pull request #39797 from quaff
* gh-39797: Polish "Add config prop for Spring Data Web's serialization mode" Add config prop for Spring Data Web's serialization mode Closes gh-39797
2 parents f37e7fe + ac4c24e commit d23528a

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-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: 34 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,24 @@ void customizeSort() {
100109
});
101110
}
102111

112+
@Test
113+
void customizePageSerializationModeViaConfigProps() {
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+
120+
@Test
121+
void customizePageSerializationModeViaCustomBean() {
122+
this.contextRunner
123+
.withBean("customSpringDataWebSettings", SpringDataWebSettings.class,
124+
() -> new SpringDataWebSettings(PageSerializationMode.VIA_DTO))
125+
.run((context) -> {
126+
assertThat(context).doesNotHaveBean("springDataWebSettings");
127+
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
128+
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
129+
});
130+
}
131+
103132
}

0 commit comments

Comments
 (0)