Skip to content

Commit b17a797

Browse files
committed
DATACMNS-802 - PagedResourceAssembler now allows customizing the PagedResource instance.
Introduced a template method to override the creation of a PagedResource to allow subclasses to create a more concrete instance than PagedResource itself.
1 parent c916fcb commit b17a797

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/main/java/org/springframework/data/web/PagedResourcesAssembler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ public Link appendPaginationParameterTemplates(Link link) {
170170
return createLink(new UriTemplate(link.getHref()), null, link.getRel());
171171
}
172172

173+
/**
174+
* Creates the {@link PagedResources} to be equipped with pagination links downstream.
175+
*
176+
* @param resources the original page's elements mapped into {@link ResourceSupport} instances.
177+
* @param metadata the calculated {@link PageMetadata}, must not be {@literal null}.
178+
* @param page the original page handed to the assembler, must not be {@literal null}.
179+
* @return must not be {@literal null}.
180+
*/
181+
protected <R extends ResourceSupport, S> PagedResources<R> createPagedResource(List<R> resources,
182+
PageMetadata metadata, Page<S> page) {
183+
184+
Assert.notNull(resources, "Content resources must not be null!");
185+
Assert.notNull(metadata, "PageMetadata must not be null!");
186+
Assert.notNull(page, "Page must not be null!");
187+
188+
return new PagedResources<R>(resources, metadata);
189+
}
190+
173191
private <S, R extends ResourceSupport> PagedResources<R> createResource(Page<S> page,
174192
ResourceAssembler<S, R> assembler, Link link) {
175193

@@ -182,7 +200,9 @@ private <S, R extends ResourceSupport> PagedResources<R> createResource(Page<S>
182200
resources.add(assembler.toResource(element));
183201
}
184202

185-
return addPaginationLinks(new PagedResources<R>(resources, asPageMetadata(page)), page, link);
203+
PagedResources<R> resource = createPagedResource(resources, asPageMetadata(page), page);
204+
205+
return addPaginationLinks(resource, page, link);
186206
}
187207

188208
private <R> PagedResources<R> addPaginationLinks(PagedResources<R> resources, Page<?> page, Link link) {

src/test/java/org/springframework/data/web/PagedResourcesAssemblerUnitTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.Collection;
2424
import java.util.Collections;
25+
import java.util.List;
2526
import java.util.Map;
2627

2728
import org.junit.Before;
@@ -33,6 +34,7 @@
3334
import org.springframework.data.domain.Pageable;
3435
import org.springframework.hateoas.Link;
3536
import org.springframework.hateoas.PagedResources;
37+
import org.springframework.hateoas.PagedResources.PageMetadata;
3638
import org.springframework.hateoas.Resource;
3739
import org.springframework.hateoas.ResourceAssembler;
3840
import org.springframework.hateoas.ResourceSupport;
@@ -273,6 +275,18 @@ public void alwaysAddsFirstAndLastLinkIfConfiguredTo() {
273275
assertThat(resources.getLink(Link.REL_LAST).getHref(), endsWith("?page=0&size=20"));
274276
}
275277

278+
/**
279+
* @see DATACMNS-802
280+
*/
281+
@Test
282+
public void usesCustomPagedResources() {
283+
284+
ResourceAssembler<Page<Person>, PagedResources<Resource<Person>>> assembler = new CustomPagedResourcesAssembler<Person>(
285+
resolver, null);
286+
287+
assertThat(assembler.toResource(EMPTY_PAGE), is(instanceOf(CustomPagedResources.class)));
288+
}
289+
276290
private static Page<Person> createPage(int index) {
277291

278292
AbstractPageRequest request = new PageRequest(index, 1);
@@ -310,4 +324,24 @@ public PersonResource toResource(Person entity) {
310324
return resource;
311325
}
312326
}
327+
328+
static class CustomPagedResourcesAssembler<T> extends PagedResourcesAssembler<T> {
329+
330+
public CustomPagedResourcesAssembler(HateoasPageableHandlerMethodArgumentResolver resolver, UriComponents baseUri) {
331+
super(resolver, baseUri);
332+
}
333+
334+
@Override
335+
protected <R extends ResourceSupport, S> PagedResources<R> createPagedResource(List<R> resources,
336+
PageMetadata metadata, Page<S> page) {
337+
return new CustomPagedResources<R>(resources, metadata);
338+
}
339+
}
340+
341+
static class CustomPagedResources<R extends ResourceSupport> extends PagedResources<R> {
342+
343+
public CustomPagedResources(Collection<R> content, PageMetadata metadata) {
344+
super(content, metadata);
345+
}
346+
}
313347
}

0 commit comments

Comments
 (0)