From 6ca349a08f955efbb71bc3bdd33ada51ba7e796c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 13 Mar 2014 08:50:52 +0100 Subject: [PATCH] DATAREST-271 - Fixed application of pagination customizations. The configuration of HateoasAwarePageableHandlerMethodArgumentResolver now gets the customizations made in RepositoryRestConfiguration applied. --- .../config/RepositoryRestConfiguration.java | 8 ++-- .../RepositoryRestMvcConfiguration.java | 34 +++++++++++++++ ...ryRestMvConfigurationIntegrationTests.java | 41 ++++++++++++++++++- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java index b493b720d..7440b2cb5 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java @@ -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; } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 2066fb771..40d929771 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -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 defaultMethodArgumentResolvers() { return Arrays.asList(pageableResolver(), sortResolver(), serverHttpRequestMethodArgumentResolver(), repoRequestArgumentResolver(), persistentEntityArgumentResolver(), diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java index e91060aca..16f0f1315 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java @@ -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 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"); + } } }