Reputation: 1870
The documentation of @PageableDefault says:
Annotation to set defaults when injecting a
org.springframework.data.domain.Pageable
into a controller method.
When using Spring Data REST, is there a way to set default values without defining a controller ?
Setting PageableDefault in the repository like below doesn't seem to work.
Page<Player> findAll(@PageableDefault(size=5) Pageable pageable);
Upvotes: 5
Views: 17251
Reputation: 1459
When using RestResource annotated repository methods, one can customize the page size by intercepting the request and adding a default parameter value (if none present):
@Component
@Order(1)
public class RestResourceDefaultPaginationFilter implements Filter {
@Value("${rest.resource.custom.page.size}")
private String pageSize;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
public String getParameter(String paramName) {
String value = super.getParameter(paramName);
// if no size parameter defined on request, then use the configuration default
if ("size".equals(paramName) && StringUtils.isEmpty(value)) {
return page.size;
}
return value;
}
}, response);
}
}
Then register this filter for specific url patterns:
@Configuration
public class RestResourcePaginationConfig {
@Bean
public FilterRegistrationBean<RestResourceDefaultPaginationFilter> paginationFilter() {
FilterRegistrationBean<RestResourceDefaultPaginationFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new RestResourceDefaultPaginationFilter());
registrationBean.addUrlPatterns("/myEntities/search/rest-resource-endpoint");
return registrationBean;
}
}
This can come handy when you want to have exceptions to the more general option described by alexbt.
Upvotes: 0
Reputation: 17045
You may extend RepositoryRestConfigurerAdapter configuration to set the default page size:
@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration) {
repositoryRestConfiguration.setDefaultPageSize(5);
}
}
spring.data.rest.default-page-size=5
# DATA REST (RepositoryRestProperties)
spring.data.rest.base-path= # Base path to be used by Spring Data REST to expose repository resources.
spring.data.rest.default-page-size= # Default size of pages.
spring.data.rest.detection-strategy=default # Strategy to use to determine which repositories get exposed.
spring.data.rest.enable-enum-translation= # Enable enum value translation via the Spring Data REST default resource bundle.
spring.data.rest.limit-param-name= # Name of the URL query string parameter that indicates how many results to return at once.
spring.data.rest.max-page-size= # Maximum size of pages.
spring.data.rest.page-param-name= # Name of the URL query string parameter that indicates what page to return.
spring.data.rest.return-body-on-create= # Return a response body after creating an entity.
spring.data.rest.return-body-on-update= # Return a response body after updating an entity.
spring.data.rest.sort-param-name= # Name of the URL query string parameter that indicates what direction to sort results.
source: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix
Upvotes: 10