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
This commit is contained in:
commit
d23528a14f
|
@ -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.
|
||||
|
@ -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,24 @@ 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
|
||||
.withBean("customSpringDataWebSettings", SpringDataWebSettings.class,
|
||||
() -> new SpringDataWebSettings(PageSerializationMode.VIA_DTO))
|
||||
.run((context) -> {
|
||||
assertThat(context).doesNotHaveBean("springDataWebSettings");
|
||||
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
|
||||
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue