From cf275cb304d24708d299f759c6b29ba37410b558 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Tue, 12 Mar 2013 09:49:39 -0500 Subject: [PATCH] Fix a bug where paging information wasn't being displayed in the JSON. --- .../rest/repository/PageableResources.java | 37 +++++++++++++++++++ .../rest/repository}/PagingAndSorting.java | 2 +- ...agingAndSortingMethodArgumentResolver.java | 2 +- .../webmvc/RepositoryEntityController.java | 28 ++++++++++++-- ...RepositoryPropertyReferenceController.java | 3 ++ .../rest/webmvc/RepositoryRestRequest.java | 2 +- ...tRequestHandlerMethodArgumentResolver.java | 2 +- .../CustomMethodArgumentResolverTests.java | 2 +- 8 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PageableResources.java rename {spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support => spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository}/PagingAndSorting.java (97%) diff --git a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PageableResources.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PageableResources.java new file mode 100644 index 000000000..6fd612c44 --- /dev/null +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PageableResources.java @@ -0,0 +1,37 @@ +package org.springframework.data.rest.repository; + +import org.springframework.data.domain.Pageable; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resources; + +/** + * @author Jon Brisbin + */ +public class PageableResources extends Resources { + + private Pageable page; + + protected PageableResources() { + super(); + } + + public PageableResources(Iterable content, Pageable page, Link... links) { + super(content, links); + this.page = page; + } + + public PageableResources(Iterable content, Pageable page, Iterable links) { + super(content, links); + this.page = page; + } + + public Pageable getPage() { + return page; + } + + public PageableResources setPage(Pageable page) { + this.page = page; + return this; + } + +} diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PagingAndSorting.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PagingAndSorting.java similarity index 97% rename from spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PagingAndSorting.java rename to spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PagingAndSorting.java index 9b7280f30..cb777eb61 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PagingAndSorting.java +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/PagingAndSorting.java @@ -1,4 +1,4 @@ -package org.springframework.data.rest.webmvc.support; +package org.springframework.data.rest.repository; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java index 869deec73..5c79ecbf9 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PagingAndSortingMethodArgumentResolver.java @@ -10,7 +10,7 @@ import org.springframework.core.MethodParameter; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.rest.config.RepositoryRestConfiguration; -import org.springframework.data.rest.webmvc.support.PagingAndSorting; +import org.springframework.data.rest.repository.PagingAndSorting; import org.springframework.data.web.PageableDefaults; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java index 9206b6032..adeee0743 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java @@ -17,6 +17,7 @@ import org.springframework.data.repository.support.DomainClassConverter; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.config.RepositoryRestConfiguration; import org.springframework.data.rest.config.ResourceMapping; +import org.springframework.data.rest.repository.PageableResources; import org.springframework.data.rest.repository.PersistentEntityResource; import org.springframework.data.rest.repository.context.AfterCreateEvent; import org.springframework.data.rest.repository.context.AfterDeleteEvent; @@ -36,6 +37,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -86,7 +88,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController } ) @ResponseBody - public Resources> listEntities(RepositoryRestRequest repoRequest) + public Resources> listEntities(final RepositoryRestRequest repoRequest) throws ResourceNotFoundException { List> resources = new ArrayList>(); List links = new ArrayList(); @@ -129,7 +131,18 @@ public class RepositoryEntityController extends AbstractRepositoryRestController repoMapping.getRel() + ".search")); } - return new Resources>(resources, links); + if(hasPagingParams || hasSortParams) { + PageRequest pr = new PageRequest(repoRequest.getPagingAndSorting().getPageNumber() + 1, + repoRequest.getPagingAndSorting().getPageSize(), + repoRequest.getPagingAndSorting().getSort()) { + @Override public int getOffset() { + return super.getOffset() - repoRequest.getPagingAndSorting().getPageSize(); + } + }; + return new PageableResources>(resources, pr, links); + } else { + return new Resources>(resources, links); + } } @SuppressWarnings({"unchecked"}) @@ -151,7 +164,13 @@ public class RepositoryEntityController extends AbstractRepositoryRestController links.add(resourceLink(repoRequest, persistentEntityResource)); } - return new Resources>(EMPTY_RESOURCE_LIST, links); + boolean hasPagingParams = (null != repoRequest.getRequest().getParameter(config.getPageParamName())); + boolean hasSortParams = (null != repoRequest.getRequest().getParameter(config.getSortParamName())); + if(hasPagingParams || hasSortParams) { + return new PageableResources>(EMPTY_RESOURCE_LIST, repoRequest.getPagingAndSorting(), links); + } else { + return new Resources>(EMPTY_RESOURCE_LIST, links); + } } @SuppressWarnings({"unchecked"}) @@ -166,6 +185,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController } ) @ResponseBody + @Transactional public ResponseEntity> createNewEntity(RepositoryRestRequest repoRequest, PersistentEntityResource incoming) { RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); @@ -251,6 +271,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController } ) @ResponseBody + @Transactional public ResponseEntity> updateEntity(RepositoryRestRequest repoRequest, PersistentEntityResource incoming, @PathVariable String id) @@ -303,6 +324,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController method = RequestMethod.DELETE ) @ResponseBody + @Transactional public ResponseEntity deleteEntity(RepositoryRestRequest repoRequest, @PathVariable String id) throws ResourceNotFoundException { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java index cc7792861..aa323b3bf 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java @@ -34,6 +34,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -261,6 +262,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes } ) @ResponseBody + @Transactional public ResponseEntity> createPropertyReference(final RepositoryRestRequest repoRequest, final @RequestBody Resource incoming, @PathVariable String id, @@ -324,6 +326,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes method = RequestMethod.DELETE ) @ResponseBody + @Transactional public ResponseEntity> deletePropertyReference(final RepositoryRestRequest repoRequest, @PathVariable String id, @PathVariable String property, diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequest.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequest.java index fb5430790..9461f4aa8 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequest.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequest.java @@ -15,7 +15,7 @@ import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.config.RepositoryRestConfiguration; import org.springframework.data.rest.config.ResourceMapping; import org.springframework.data.rest.repository.invoke.RepositoryMethodInvoker; -import org.springframework.data.rest.webmvc.support.PagingAndSorting; +import org.springframework.data.rest.repository.PagingAndSorting; import org.springframework.hateoas.Link; import org.springframework.web.util.UriComponentsBuilder; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequestHandlerMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequestHandlerMethodArgumentResolver.java index cb6a32bbb..228bfdb66 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequestHandlerMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestRequestHandlerMethodArgumentResolver.java @@ -8,7 +8,7 @@ import org.springframework.core.MethodParameter; import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.config.RepositoryRestConfiguration; -import org.springframework.data.rest.webmvc.support.PagingAndSorting; +import org.springframework.data.rest.repository.PagingAndSorting; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomMethodArgumentResolverTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomMethodArgumentResolverTests.java index 4b53b0c86..577578695 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomMethodArgumentResolverTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomMethodArgumentResolverTests.java @@ -10,7 +10,7 @@ import org.junit.Test; import org.springframework.core.MethodParameter; import org.springframework.data.rest.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.annotation.BaseURI; -import org.springframework.data.rest.webmvc.support.PagingAndSorting; +import org.springframework.data.rest.repository.PagingAndSorting; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.method.support.ModelAndViewContainer;