Fix a bug where paging information wasn't being displayed in the JSON.

This commit is contained in:
Jon Brisbin
2013-03-12 09:49:39 -05:00
committed by Jon Brisbin
parent df239e49ce
commit cf275cb304
8 changed files with 70 additions and 8 deletions

View File

@@ -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<T> extends Resources<T> {
private Pageable page;
protected PageableResources() {
super();
}
public PageableResources(Iterable<T> content, Pageable page, Link... links) {
super(content, links);
this.page = page;
}
public PageableResources(Iterable<T> content, Pageable page, Iterable<Link> links) {
super(content, links);
this.page = page;
}
public Pageable getPage() {
return page;
}
public PageableResources<T> setPage(Pageable page) {
this.page = page;
return this;
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<Resource<?>> listEntities(RepositoryRestRequest repoRequest)
public Resources<Resource<?>> listEntities(final RepositoryRestRequest repoRequest)
throws ResourceNotFoundException {
List<Resource<?>> resources = new ArrayList<Resource<?>>();
List<Link> links = new ArrayList<Link>();
@@ -129,7 +131,18 @@ public class RepositoryEntityController extends AbstractRepositoryRestController
repoMapping.getRel() + ".search"));
}
return new Resources<Resource<?>>(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<Resource<?>>(resources, pr, links);
} else {
return new Resources<Resource<?>>(resources, links);
}
}
@SuppressWarnings({"unchecked"})
@@ -151,7 +164,13 @@ public class RepositoryEntityController extends AbstractRepositoryRestController
links.add(resourceLink(repoRequest, persistentEntityResource));
}
return new Resources<Resource<?>>(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<Resource<?>>(EMPTY_RESOURCE_LIST, repoRequest.getPagingAndSorting(), links);
} else {
return new Resources<Resource<?>>(EMPTY_RESOURCE_LIST, links);
}
}
@SuppressWarnings({"unchecked"})
@@ -166,6 +185,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController
}
)
@ResponseBody
@Transactional
public ResponseEntity<Resource<?>> createNewEntity(RepositoryRestRequest repoRequest,
PersistentEntityResource<?> incoming) {
RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker();
@@ -251,6 +271,7 @@ public class RepositoryEntityController extends AbstractRepositoryRestController
}
)
@ResponseBody
@Transactional
public ResponseEntity<Resource<?>> 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 {

View File

@@ -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<Resource<?>> createPropertyReference(final RepositoryRestRequest repoRequest,
final @RequestBody Resource<Object> incoming,
@PathVariable String id,
@@ -324,6 +326,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes
method = RequestMethod.DELETE
)
@ResponseBody
@Transactional
public ResponseEntity<Resource<?>> deletePropertyReference(final RepositoryRestRequest repoRequest,
@PathVariable String id,
@PathVariable String property,

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;