如何在 Spring Boot 中设置 Max Pagination Size?
How to set Max Pagination Size in Spring Boot?
我正在开发 Spring Boot + Spring Data Mongo + Spring HATEOAS
示例。我正在使用 Spring 启动 V2.2.2.RELEASE
。
我在全球范围内尝试将分页页面大小限制设置为 200。为此,我经历了 spring data jpa limit pagesize, how to set to maxSize 和如下配置
# DATA WEB (SpringDataWebProperties)
spring.data.web.pageable.default-page-size=20 # Default page size.
spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page # Page index parameter name.
spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size # Page size parameter name.
spring.data.web.sort.sort-parameter=sort # Sort parameter name.
如果客户端发送超过 200 PageSize,那么我需要显示用户友好的错误消息。
@Configuration
public class PaginationConfig extends SpringDataWebConfiguration{
public PaginationConfig(ApplicationContext context, ObjectFactory<ConversionService> conversionService) {
super(context, conversionService);
}
@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver(sortResolver());
resolver.setMaxPageSize(1000);
return resolver;
}
}
我使用了上面的配置,但是出现以下错误。
2020-02-06 21:08:31.963 ERROR [Reference Data,,,] 22812 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field pagedAssembler in com.example.EmployeeController required a bean of type 'org.springframework.data.web.PagedResourcesAssembler' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.data.web.PagedResourcesAssembler' in your configuration.
代码:
@GetMapping(value = "/countries", produces = { MediaType.APPLICATION_JSON })
public ResponseEntity<PagedModel<EmployeeModel>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
Page<EmployeeDto> page = EmployeeService.findAllEmployees(page_params, pageable);
PagedModel<EmployeeModel> model = pagedAssembler.toModel(page, EmployeeAssembler);
return new ResponseEntity<>(model, HttpStatus.OK);
}
我能够使用以下代码解决问题。
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver() {
@Override
public Pageable resolveArgument(MethodParameter methodParameter,
@Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
@Nullable WebDataBinderFactory binderFactory) {
Pageable p = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
if(p.getPageSize() > Integer.parseInt(500)) {
throw new MandatoryResourceException("Error"), env.getProperty("500"));
}
return p;
}
};
resolvers.add(resolver);
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
}
}
我正在开发 Spring Boot + Spring Data Mongo + Spring HATEOAS
示例。我正在使用 Spring 启动 V2.2.2.RELEASE
。
我在全球范围内尝试将分页页面大小限制设置为 200。为此,我经历了 spring data jpa limit pagesize, how to set to maxSize 和如下配置
# DATA WEB (SpringDataWebProperties)
spring.data.web.pageable.default-page-size=20 # Default page size.
spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page # Page index parameter name.
spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size # Page size parameter name.
spring.data.web.sort.sort-parameter=sort # Sort parameter name.
如果客户端发送超过 200 PageSize,那么我需要显示用户友好的错误消息。
@Configuration
public class PaginationConfig extends SpringDataWebConfiguration{
public PaginationConfig(ApplicationContext context, ObjectFactory<ConversionService> conversionService) {
super(context, conversionService);
}
@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver(sortResolver());
resolver.setMaxPageSize(1000);
return resolver;
}
}
我使用了上面的配置,但是出现以下错误。
2020-02-06 21:08:31.963 ERROR [Reference Data,,,] 22812 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field pagedAssembler in com.example.EmployeeController required a bean of type 'org.springframework.data.web.PagedResourcesAssembler' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.data.web.PagedResourcesAssembler' in your configuration.
代码:
@GetMapping(value = "/countries", produces = { MediaType.APPLICATION_JSON })
public ResponseEntity<PagedModel<EmployeeModel>> findEmployees(@Parameter(hidden=true) Pageable pageable) {
Page<EmployeeDto> page = EmployeeService.findAllEmployees(page_params, pageable);
PagedModel<EmployeeModel> model = pagedAssembler.toModel(page, EmployeeAssembler);
return new ResponseEntity<>(model, HttpStatus.OK);
}
我能够使用以下代码解决问题。
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver() {
@Override
public Pageable resolveArgument(MethodParameter methodParameter,
@Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
@Nullable WebDataBinderFactory binderFactory) {
Pageable p = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
if(p.getPageSize() > Integer.parseInt(500)) {
throw new MandatoryResourceException("Error"), env.getProperty("500"));
}
return p;
}
};
resolvers.add(resolver);
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
}
}