Add config prop for Spring Data Web's serialization mode
See gh-39797
This commit is contained in:
committed by
Andy Wilkinson
parent
f37e7fef23
commit
81c903cde7
@@ -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.
|
||||
@@ -31,6 +31,7 @@ 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 +43,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 +81,10 @@ public class SpringDataWebAutoConfiguration {
|
||||
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SpringDataWebSettings springDataWebSettings() {
|
||||
return new SpringDataWebSettings(this.properties.getPageable().getSerializationMode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
@@ -21,9 +21,13 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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 +37,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 +58,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 +75,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 +85,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 +98,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 +111,32 @@ class SpringDataWebAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizePageSerializationModeViaConfigProps() {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizePageSerializationModeViaCustomBean() {
|
||||
this.contextRunner.withUserConfiguration(AppConfiguration.class)
|
||||
.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO")
|
||||
.run((context) -> {
|
||||
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
|
||||
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.DIRECT);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class AppConfiguration {
|
||||
|
||||
@Bean
|
||||
SpringDataWebSettings springDataWebSettings() {
|
||||
return new SpringDataWebSettings(PageSerializationMode.DIRECT);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user