DATACMNS-1470 - Adapt to package refactorings in Spring HATEOAS.

This commit is contained in:
Oliver Drotbohm
2019-02-27 12:59:10 +01:00
parent 06cb767f84
commit 435aaaaa35
9 changed files with 88 additions and 83 deletions

View File

@@ -35,7 +35,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -96,7 +96,7 @@ public abstract class SpringDataJaxb {
*/
@XmlRootElement(name = "page", namespace = NAMESPACE)
@XmlAccessorType(XmlAccessType.FIELD)
public static class PageDto extends ResourceSupport {
public static class PageDto extends RepresentationModel {
@Nullable @XmlAnyElement @XmlElementWrapper(name = "content") List<Object> content;
}

View File

@@ -26,7 +26,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.TemplateVariable;
import org.springframework.hateoas.TemplateVariable.VariableType;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.mvc.UriComponentsContributor;
import org.springframework.hateoas.server.mvc.UriComponentsContributor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
@@ -100,7 +100,7 @@ public class HateoasPageableHandlerMethodArgumentResolver extends PageableHandle
/*
* (non-Javadoc)
* @see org.springframework.hateoas.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object)
* @see org.springframework.hateoas.server.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object)
*/
@Override
public void enhance(UriComponentsBuilder builder, @Nullable MethodParameter parameter, Object value) {

View File

@@ -22,7 +22,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.hateoas.TemplateVariable;
import org.springframework.hateoas.TemplateVariable.VariableType;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.mvc.UriComponentsContributor;
import org.springframework.hateoas.server.mvc.UriComponentsContributor;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -64,7 +64,7 @@ public class HateoasSortHandlerMethodArgumentResolver extends SortHandlerMethodA
/*
* (non-Javadoc)
* @see org.springframework.hateoas.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object)
* @see org.springframework.hateoas.server.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object)
*/
@Override
public void enhance(UriComponentsBuilder builder, MethodParameter parameter, Object value) {

View File

@@ -26,17 +26,17 @@ import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.PagedResources.PageMetadata;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceAssembler;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.UriTemplate;
import org.springframework.hateoas.core.EmbeddedWrapper;
import org.springframework.hateoas.core.EmbeddedWrappers;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.hateoas.server.core.EmbeddedWrapper;
import org.springframework.hateoas.server.core.EmbeddedWrappers;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -51,7 +51,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Nick Williams
* @author Marcel Overdijk
*/
public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, PagedResources<Resource<T>>> {
public class PagedResourcesAssembler<T> implements RepresentationModelAssembler<Page<T>, PagedModel<EntityModel<T>>> {
private final HateoasPageableHandlerMethodArgumentResolver pageableResolver;
private final Optional<UriComponents> baseUri;
@@ -62,7 +62,7 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
/**
* Creates a new {@link PagedResourcesAssembler} using the given {@link PageableHandlerMethodArgumentResolver} and
* base URI. If the former is {@literal null}, a default one will be created. If the latter is {@literal null}, calls
* to {@link #toResource(Page)} will use the current request's URI to build the relevant previous and next links.
* to {@link #toModel(Page)} will use the current request's URI to build the relevant previous and next links.
*
* @param resolver can be {@literal null}.
* @param baseUri can be {@literal null}.
@@ -89,12 +89,12 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceAssembler#toResource(java.lang.Object)
* @see org.springframework.hateoas.server.RepresentationModelAssembler#toModel(java.lang.Object)
*/
@Override
@SuppressWarnings("null")
public PagedResources<Resource<T>> toResource(Page<T> entity) {
return toResource(entity, it -> new Resource<>(it));
public PagedModel<EntityModel<T>> toModel(Page<T> entity) {
return toModel(entity, it -> new EntityModel<>(it));
}
/**
@@ -106,8 +106,8 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @param selfLink must not be {@literal null}.
* @return
*/
public PagedResources<Resource<T>> toResource(Page<T> page, Link selfLink) {
return toResource(page, it -> new Resource<>(it), selfLink);
public PagedModel<EntityModel<T>> toModel(Page<T> page, Link selfLink) {
return toModel(page, it -> new EntityModel<>(it), selfLink);
}
/**
@@ -118,8 +118,9 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @param assembler must not be {@literal null}.
* @return
*/
public <R extends ResourceSupport> PagedResources<R> toResource(Page<T> page, ResourceAssembler<T, R> assembler) {
return createResource(page, assembler, Optional.empty());
public <R extends RepresentationModel<?>> PagedModel<R> toModel(Page<T> page,
RepresentationModelAssembler<T, R> assembler) {
return createModel(page, assembler, Optional.empty());
}
/**
@@ -132,12 +133,12 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @param link must not be {@literal null}.
* @return
*/
public <R extends ResourceSupport> PagedResources<R> toResource(Page<T> page, ResourceAssembler<T, R> assembler,
Link link) {
public <R extends RepresentationModel<?>> PagedModel<R> toModel(Page<T> page,
RepresentationModelAssembler<T, R> assembler, Link link) {
Assert.notNull(link, "Link must not be null!");
return createResource(page, assembler, Optional.of(link));
return createModel(page, assembler, Optional.of(link));
}
/**
@@ -148,8 +149,8 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @return
* @since 2.0
*/
public PagedResources<?> toEmptyResource(Page<?> page, Class<?> type) {
return toEmptyResource(page, type, Optional.empty());
public PagedModel<?> toEmptyModel(Page<?> page, Class<?> type) {
return toEmptyModel(page, type, Optional.empty());
}
/**
@@ -161,11 +162,11 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @return
* @since 1.11
*/
public PagedResources<?> toEmptyResource(Page<?> page, Class<?> type, Link link) {
return toEmptyResource(page, type, Optional.of(link));
public PagedModel<?> toEmptyModel(Page<?> page, Class<?> type, Link link) {
return toEmptyModel(page, type, Optional.of(link));
}
private PagedResources<?> toEmptyResource(Page<?> page, Class<?> type, Optional<Link> link) {
private PagedModel<?> toEmptyModel(Page<?> page, Class<?> type, Optional<Link> link) {
Assert.notNull(page, "Page must must not be null!");
Assert.isTrue(!page.hasContent(), "Page must not have any content!");
@@ -177,7 +178,7 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
EmbeddedWrapper wrapper = wrappers.emptyCollectionOf(type);
List<EmbeddedWrapper> embedded = Collections.singletonList(wrapper);
return addPaginationLinks(new PagedResources<>(embedded, metadata), page, link);
return addPaginationLinks(new PagedModel<>(embedded, metadata), page, link);
}
/**
@@ -188,18 +189,18 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
* @param page the original page handed to the assembler, must not be {@literal null}.
* @return must not be {@literal null}.
*/
protected <R extends ResourceSupport, S> PagedResources<R> createPagedResource(List<R> resources,
protected <R extends RepresentationModel<?>, S> PagedModel<R> createPagedModel(List<R> resources,
PageMetadata metadata, Page<S> page) {
Assert.notNull(resources, "Content resources must not be null!");
Assert.notNull(metadata, "PageMetadata must not be null!");
Assert.notNull(page, "Page must not be null!");
return new PagedResources<>(resources, metadata);
return new PagedModel<>(resources, metadata);
}
private <S, R extends ResourceSupport> PagedResources<R> createResource(Page<S> page,
ResourceAssembler<S, R> assembler, Optional<Link> link) {
private <S, R extends RepresentationModel<?>> PagedModel<R> createModel(Page<S> page,
RepresentationModelAssembler<S, R> assembler, Optional<Link> link) {
Assert.notNull(page, "Page must not be null!");
Assert.notNull(assembler, "ResourceAssembler must not be null!");
@@ -207,15 +208,15 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
List<R> resources = new ArrayList<>(page.getNumberOfElements());
for (S element : page) {
resources.add(assembler.toResource(element));
resources.add(assembler.toModel(element));
}
PagedResources<R> resource = createPagedResource(resources, asPageMetadata(page), page);
PagedModel<R> resource = createPagedModel(resources, asPageMetadata(page), page);
return addPaginationLinks(resource, page, link);
}
private <R> PagedResources<R> addPaginationLinks(PagedResources<R> resources, Page<?> page, Optional<Link> link) {
private <R> PagedModel<R> addPaginationLinks(PagedModel<R> resources, Page<?> page, Optional<Link> link) {
UriTemplate base = getUriTemplate(link);

View File

@@ -25,9 +25,9 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MethodLinkBuilderFactory;
import org.springframework.hateoas.core.MethodParameters;
import org.springframework.hateoas.mvc.ControllerLinkBuilderFactory;
import org.springframework.hateoas.server.MethodLinkBuilderFactory;
import org.springframework.hateoas.server.core.MethodParameters;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilderFactory;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
@@ -60,13 +60,13 @@ public class PagedResourcesAssemblerArgumentResolver implements HandlerMethodArg
* {@link PageableHandlerMethodArgumentResolver} and {@link MethodLinkBuilderFactory}.
*
* @param resolver can be {@literal null}.
* @param linkBuilderFactory can be {@literal null}, will be defaulted to a {@link ControllerLinkBuilderFactory}.
* @param linkBuilderFactory can be {@literal null}, will be defaulted to a {@link WebMvcLinkBuilderFactory}.
*/
public PagedResourcesAssemblerArgumentResolver(HateoasPageableHandlerMethodArgumentResolver resolver,
@Nullable MethodLinkBuilderFactory<?> linkBuilderFactory) {
this.resolver = resolver;
this.linkBuilderFactory = linkBuilderFactory == null ? new ControllerLinkBuilderFactory() : linkBuilderFactory;
this.linkBuilderFactory = linkBuilderFactory == null ? new WebMvcLinkBuilderFactory() : linkBuilderFactory;
}
/*

View File

@@ -23,7 +23,7 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.mvc.UriComponentsContributor;
import org.springframework.hateoas.server.mvc.UriComponentsContributor;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

View File

@@ -30,14 +30,14 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.PagedResources.PageMetadata;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceAssembler;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.core.EmbeddedWrapper;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.hateoas.server.core.EmbeddedWrapper;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -64,7 +64,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test
public void addsNextLinkForFirstPage() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(0));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(0));
assertThat(resources.getLink(IanaLinkRelations.PREV)).isEmpty();
assertThat(resources.getLink(IanaLinkRelations.SELF)).isNotEmpty();
@@ -74,7 +74,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test
public void addsPreviousAndNextLinksForMiddlePage() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1));
assertThat(resources.getLink(IanaLinkRelations.PREV)).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelations.SELF)).isNotEmpty();
@@ -84,7 +84,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test
public void addsPreviousLinkForLastPage() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(2));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(2));
assertThat(resources.getLink(IanaLinkRelations.PREV)).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelations.SELF)).isNotEmpty();
@@ -97,7 +97,7 @@ public class PagedResourcesAssemblerUnitTests {
UriComponents baseUri = UriComponentsBuilder.fromUriString("http://foo:9090").build();
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<>(resolver, baseUri);
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1));
assertThat(resources.getRequiredLink(IanaLinkRelations.PREV).getHref()).startsWith(baseUri.toUriString());
assertThat(resources.getRequiredLink(IanaLinkRelations.SELF)).isNotNull();
@@ -109,7 +109,7 @@ public class PagedResourcesAssemblerUnitTests {
Link link = new Link("http://foo:9090", "rel");
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1), link);
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1), link);
assertThat(resources.getRequiredLink(IanaLinkRelations.PREV).getHref()).startsWith(link.getHref());
assertThat(resources.getRequiredLink(IanaLinkRelations.SELF)).isEqualTo(link.withSelfRel());
@@ -124,13 +124,13 @@ public class PagedResourcesAssemblerUnitTests {
AbstractPageRequest request = PageRequest.of(0, 1);
Page<Person> page = new PageImpl<>(Collections.emptyList(), request, 0);
assembler.toResource(page);
assembler.toModel(page);
}
@Test // DATACMNS-418, DATACMNS-515
public void createsACanonicalLinkWithoutTemplateParameters() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1));
assertThat(resources.getRequiredLink(IanaLinkRelations.SELF).getHref()).doesNotContain("{").doesNotContain("}");
}
@@ -140,7 +140,7 @@ public class PagedResourcesAssemblerUnitTests {
PersonResourceAssembler personAssembler = new PersonResourceAssembler();
PagedResources<PersonResource> resources = assembler.toResource(createPage(0), personAssembler);
PagedModel<PersonResource> resources = assembler.toModel(createPage(0), personAssembler);
assertThat(resources.hasLink(IanaLinkRelations.SELF)).isTrue();
assertThat(resources.hasLink(IanaLinkRelations.NEXT)).isTrue();
@@ -156,7 +156,7 @@ public class PagedResourcesAssemblerUnitTests {
argumentResolver.setOneIndexedParameters(true);
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<>(argumentResolver, null);
PagedResources<Resource<Person>> resource = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resource = assembler.toModel(createPage(1));
assertThat(resource.hasLink("prev")).isTrue();
assertThat(resource.hasLink("next")).isTrue();
@@ -171,7 +171,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-515
public void generatedLinksShouldNotBeTemplated() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1));
assertThat(resources.getRequiredLink(IanaLinkRelations.SELF).getHref()).doesNotContain("{").doesNotContain("}");
assertThat(resources.getRequiredLink(IanaLinkRelations.NEXT).getHref()).endsWith("?page=2&size=1");
@@ -181,7 +181,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-699
public void generatesEmptyPagedResourceWithEmbeddedWrapper() {
PagedResources<?> result = assembler.toEmptyResource(EMPTY_PAGE, Person.class);
PagedModel<?> result = assembler.toEmptyModel(EMPTY_PAGE, Person.class);
Collection<?> content = result.getContent();
assertThat(content).hasSize(1);
@@ -193,18 +193,18 @@ public class PagedResourcesAssemblerUnitTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-699
public void emptyPageCreatorRejectsPageWithContent() {
assembler.toEmptyResource(createPage(1), Person.class);
assembler.toEmptyModel(createPage(1), Person.class);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-699
public void emptyPageCreatorRejectsNullType() {
assembler.toEmptyResource(EMPTY_PAGE, null);
assembler.toEmptyModel(EMPTY_PAGE, null);
}
@Test // DATACMNS-701
public void addsFirstAndLastLinksForMultiplePages() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(1));
assertThat(resources.getRequiredLink(IanaLinkRelations.FIRST).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelations.LAST).getHref()).endsWith("?page=2&size=1");
@@ -213,7 +213,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-701
public void addsFirstAndLastLinksForFirstPage() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(0));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(0));
assertThat(resources.getRequiredLink(IanaLinkRelations.FIRST).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelations.LAST).getHref()).endsWith("?page=2&size=1");
@@ -222,7 +222,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-701
public void addsFirstAndLastLinksForLastPage() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(2));
PagedModel<EntityModel<Person>> resources = assembler.toModel(createPage(2));
assertThat(resources.getRequiredLink(IanaLinkRelations.FIRST).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelations.LAST).getHref()).endsWith("?page=2&size=1");
@@ -234,7 +234,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<>(resolver, null);
assembler.setForceFirstAndLastRels(true);
PagedResources<Resource<Person>> resources = assembler.toResource(EMPTY_PAGE);
PagedModel<EntityModel<Person>> resources = assembler.toModel(EMPTY_PAGE);
assertThat(resources.getRequiredLink(IanaLinkRelations.FIRST).getHref()).endsWith("?page=0&size=20");
assertThat(resources.getRequiredLink(IanaLinkRelations.LAST).getHref()).endsWith("?page=0&size=20");
@@ -243,16 +243,16 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-802
public void usesCustomPagedResources() {
ResourceAssembler<Page<Person>, PagedResources<Resource<Person>>> assembler = new CustomPagedResourcesAssembler<>(
RepresentationModelAssembler<Page<Person>, PagedModel<EntityModel<Person>>> assembler = new CustomPagedResourcesAssembler<>(
resolver, null);
assertThat(assembler.toResource(EMPTY_PAGE)).isInstanceOf(CustomPagedResources.class);
assertThat(assembler.toModel(EMPTY_PAGE)).isInstanceOf(CustomPagedResources.class);
}
@Test // DATACMNS-1042
public void selfLinkContainsCoordinatesForCurrentPage() {
PagedResources<Resource<Person>> resource = assembler.toResource(createPage(0));
PagedModel<EntityModel<Person>> resource = assembler.toModel(createPage(0));
assertThat(resource.getRequiredLink(IanaLinkRelations.SELF).getHref()).endsWith("?page=0&size=1");
}
@@ -277,18 +277,18 @@ public class PagedResourcesAssemblerUnitTests {
String name;
}
static class PersonResource extends ResourceSupport {
static class PersonResource extends RepresentationModel<PersonResource> {
String name;
}
static class PersonResourceAssembler implements ResourceAssembler<Person, PersonResource> {
static class PersonResourceAssembler implements RepresentationModelAssembler<Person, PersonResource> {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.ResourceAssembler#toResource(java.lang.Object)
* @see org.springframework.hateoas.server.RepresentationModelAssembler#toModel(java.lang.Object)
*/
@Override
public PersonResource toResource(Person entity) {
public PersonResource toModel(Person entity) {
PersonResource resource = new PersonResource();
resource.name = entity.name;
return resource;
@@ -301,14 +301,18 @@ public class PagedResourcesAssemblerUnitTests {
super(resolver, baseUri);
}
/*
* (non-Javadoc)
* @see org.springframework.data.web.PagedResourcesAssembler#createPagedModel(java.util.List, org.springframework.hateoas.PagedModel.PageMetadata, org.springframework.data.domain.Page)
*/
@Override
protected <R extends ResourceSupport, S> PagedResources<R> createPagedResource(List<R> resources,
protected <R extends RepresentationModel<?>, S> PagedModel<R> createPagedModel(List<R> resources,
PageMetadata metadata, Page<S> page) {
return new CustomPagedResources<>(resources, metadata);
}
}
static class CustomPagedResources<R extends ResourceSupport> extends PagedResources<R> {
static class CustomPagedResources<R extends RepresentationModel> extends PagedModel<R> {
public CustomPagedResources(Collection<R> content, PageMetadata metadata) {
super(content, metadata);

View File

@@ -32,8 +32,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.data.web.WebTestUtils;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
@@ -68,7 +68,7 @@ public class PageableResourcesAssemblerIntegrationTests {
assertThat(controller.assembler).isNotNull();
PagedResources<Resource<Person>> resources = controller.sample(PageRequest.of(1, 1));
PagedModel<EntityModel<Person>> resources = controller.sample(PageRequest.of(1, 1));
assertThat(resources.getLink(IanaLinkRelations.PREV)).isNotNull();
assertThat(resources.getLink(IanaLinkRelations.NEXT)).isNotNull();
@@ -97,12 +97,12 @@ public class PageableResourcesAssemblerIntegrationTests {
@Autowired PagedResourcesAssembler<Person> assembler;
@RequestMapping("/persons")
PagedResources<Resource<Person>> sample(Pageable pageable) {
PagedModel<EntityModel<Person>> sample(Pageable pageable) {
Page<Person> page = new PageImpl<>(Collections.singletonList(new Person()), pageable,
pageable.getOffset() + pageable.getPageSize() + 1);
return assembler.toResource(page);
return assembler.toModel(page);
}
}

View File

@@ -36,7 +36,7 @@ import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -246,7 +246,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
ModelAndView forModelAndView();
ResponseEntity<Resource<User>> forResourceOfUser();
ResponseEntity<EntityModel<User>> forResourceOfUser();
}
public static class SampleRepo implements QuerydslBinderCustomizer<QUser> {