DATAREST-271 - Fixed application of pagination customizations.

The configuration of HateoasAwarePageableHandlerMethodArgumentResolver now gets the customizations made in RepositoryRestConfiguration applied.
This commit is contained in:
Oliver Gierke
2014-03-13 08:50:52 +01:00
parent 11b94b9d1a
commit 6ca349a08f
3 changed files with 77 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -35,7 +35,7 @@ public class RepositoryRestConfiguration {
private int defaultPageSize = 20;
private int maxPageSize = 1000;
private String pageParamName = "page";
private String limitParamName = "limit";
private String limitParamName = "size";
private String sortParamName = "sort";
private MediaType defaultMediaType = MediaTypes.HAL_JSON;
private boolean returnBodyOnCreate = false;
@@ -80,7 +80,7 @@ public class RepositoryRestConfiguration {
* @return {@literal this}
*/
public RepositoryRestConfiguration setDefaultPageSize(int defaultPageSize) {
Assert.isTrue((defaultPageSize > 0), "Page size must be greater than 0.");
Assert.isTrue(defaultPageSize > 0, "Page size must be greater than 0.");
this.defaultPageSize = defaultPageSize;
return this;
}
@@ -101,7 +101,7 @@ public class RepositoryRestConfiguration {
* @return {@literal this}
*/
public RepositoryRestConfiguration setMaxPageSize(int maxPageSize) {
Assert.isTrue((defaultPageSize > 0), "Maximum page size must be greater than 0.");
Assert.isTrue(defaultPageSize > 0, "Maximum page size must be greater than 0.");
this.maxPageSize = maxPageSize;
return this;
}

View File

@@ -34,6 +34,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.DomainClassConverter;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.UriToEntityConverter;
@@ -64,6 +65,8 @@ import org.springframework.data.rest.webmvc.support.HttpMethodHandlerMethodArgum
import org.springframework.data.rest.webmvc.support.JpaHelper;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.data.rest.webmvc.support.ValidationExceptionHandler;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver;
import org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.hateoas.EntityLinks;
@@ -484,6 +487,37 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
return messageConverters;
}
/*
* (non-Javadoc)
* @see org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration#pageableResolver()
*/
@Bean
@Override
public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
HateoasPageableHandlerMethodArgumentResolver resolver = super.pageableResolver();
resolver.setPageParameterName(config().getPageParamName());
resolver.setSizeParameterName(config().getLimitParamName());
resolver.setFallbackPageable(new PageRequest(0, config().getDefaultPageSize()));
resolver.setMaxPageSize(config().getMaxPageSize());
return resolver;
}
/*
* (non-Javadoc)
* @see org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration#sortResolver()
*/
@Bean
@Override
public HateoasSortHandlerMethodArgumentResolver sortResolver() {
HateoasSortHandlerMethodArgumentResolver resolver = super.sortResolver();
resolver.setSortParameter(config().getSortParamName());
return resolver;
}
private List<HandlerMethodArgumentResolver> defaultMethodArgumentResolvers() {
return Arrays.asList(pageableResolver(), sortResolver(), serverHttpRequestMethodArgumentResolver(),
repoRequestArgumentResolver(), persistentEntityArgumentResolver(),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -15,7 +15,7 @@
*/
package org.springframework.data.rest.webmvc.config;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.AfterClass;
@@ -25,11 +25,17 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.hateoas.LinkDiscoverers;
import org.springframework.hateoas.core.DefaultRelProvider;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -75,6 +81,27 @@ public class RepositoryRestMvConfigurationIntegrationTests {
mapper.writeValueAsString(new RepositoryLinksResource());
}
/**
* @see DATAREST-271
*/
@Test
public void assetConsidersPaginationCustomization() {
HateoasPageableHandlerMethodArgumentResolver resolver = context
.getBean(HateoasPageableHandlerMethodArgumentResolver.class);
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
resolver.enhance(builder, null, new PageRequest(0, 9000, Direction.ASC, "firstname"));
MultiValueMap<String, String> params = builder.build().getQueryParams();
assertThat(params.containsKey("myPage"), is(true));
assertThat(params.containsKey("mySort"), is(true));
assertThat(params.get("mySize"), hasSize(1));
assertThat(params.get("mySize").get(0), is("7000"));
}
@Configuration
static class ExtendingConfiguration extends RepositoryRestMvcConfiguration {
@@ -82,5 +109,15 @@ public class RepositoryRestMvConfigurationIntegrationTests {
public DefaultRelProvider relProvider() {
return new DefaultRelProvider();
}
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(45);
config.setMaxPageSize(7000);
config.setPageParamName("myPage");
config.setLimitParamName("mySize");
config.setSortParamName("mySort");
}
}
}