From af6f55a92c5e87037f879c8a0b6ed4026882bf3c Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Mon, 26 Feb 2018 11:53:22 -0600 Subject: [PATCH] DATAREST-1341 - Upgrade to Spring HATEOAS 1.0. --- pom.xml | 1 - spring-data-rest-core/pom.xml | 2 +- .../rest/core/util/Java8PluginRegistry.java | 2 +- .../data/rest/tests/ResourceTester.java | 2 +- ...RepositoryEntityLinksIntegrationTests.java | 2 +- ...tityResourceAssemblerIntegrationTests.java | 4 +-- .../AbstractRepositoryRestController.java | 2 +- .../PersistentEntityResourceAssembler.java | 10 +++----- .../rest/webmvc/RepositoryController.java | 2 +- .../webmvc/RepositoryEntityController.java | 2 +- ...RepositoryPropertyReferenceController.java | 14 +++++------ .../webmvc/RepositorySearchController.java | 25 ++++++++----------- ...eInformationToAlpsDescriptorConverter.java | 14 +++++------ .../RepositoryRestMvcConfiguration.java | 4 +-- .../json/PersistentEntityJackson2Module.java | 2 +- .../rest/webmvc/mapping/LinkCollector.java | 2 +- 16 files changed, 42 insertions(+), 48 deletions(-) diff --git a/pom.xml b/pom.xml index 527776d30..8cc9817ea 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,6 @@ 4.1.0.BUILD-SNAPSHOT 2.2.0.BUILD-SNAPSHOT 2.2.0.BUILD-SNAPSHOT - 0.25.0.RELEASE 5.2.17.Final 1.1.0 diff --git a/spring-data-rest-core/pom.xml b/spring-data-rest-core/pom.xml index ff3116b07..5ab0ad2b2 100644 --- a/spring-data-rest-core/pom.xml +++ b/spring-data-rest-core/pom.xml @@ -16,7 +16,7 @@ - 1.2.0.RELEASE + 2.0.0.BUILD-SNAPSHOT 1.2.2 spring.data.rest.core ${basedir}/.. diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java index 42ca48117..0b134bfa8 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java @@ -46,7 +46,7 @@ public class Java8PluginRegistry, S> { } public Optional getPluginFor(S delimiter) { - return Optional.ofNullable(registry.getPluginFor(delimiter)); + return registry.getPluginFor(delimiter); } public T getPluginOrDefaultFor(S delimiter, T fallback) { diff --git a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java index 64e4c3bf7..2af3fc50c 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java +++ b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java @@ -83,7 +83,7 @@ public class ResourceTester { private final Link assertHasLinkMatching(String rel, Matcher hrefMatcher) { - Link link = resource.getLink(rel); + Link link = resource.getRequiredLink(rel); assertThat("Expected link with rel '" + rel + "' but didn't find it in " + resource.getLinks(), link, is(notNullValue())); diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java index 85e8ee387..a76043ae7 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java @@ -99,7 +99,7 @@ public class RepositoryEntityLinksIntegrationTests extends AbstractControllerInt assertThat(links.hasLink("firstname")).isTrue(); - Link firstnameLink = links.getLink("firstname"); + Link firstnameLink = links.getLink("firstname").orElse(null); assertThat(firstnameLink.isTemplated()).isTrue(); assertThat(firstnameLink.getVariableNames()).contains("page", "size"); } diff --git a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java index cb55fa5e8..fa8c77633 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java @@ -68,7 +68,7 @@ public class PersistentEntityResourceAssemblerIntegrationTests extends AbstractC Links links = new Links(resource.getLinks()); assertThat(links).hasSize(2); - assertThat(links.getLink("self").getVariables()).isEmpty(); - assertThat(links.getLink("user").getVariableNames()).contains("projection"); + assertThat(links.getLink("self").orElseThrow(() -> new RuntimeException("Unable to find 'self' link")).getVariables()).isEmpty(); + assertThat(links.getLink("user").orElseThrow(() -> new RuntimeException("Unable to find 'user' link")).getVariableNames()).contains("projection"); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java index 56972f4fa..0e7924ec8 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java @@ -63,7 +63,7 @@ class AbstractRepositoryRestController { ResourceMetadata repoMapping = resourceLink.getResourceMetadata(); - Link selfLink = resource.getLink("self"); + Link selfLink = resource.getRequiredLink(Link.REL_SELF); String rel = repoMapping.getItemResourceRel(); return new Link(selfLink.getHref(), rel); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java index 3e1b8038f..b58a8808f 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java @@ -73,7 +73,7 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler(resource, HttpStatus.OK); + return new ResponseEntity<>(resource, HttpStatus.OK); } } 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 5bb64c83a..e2893de61 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 @@ -499,7 +499,7 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem */ private void addLocationHeader(HttpHeaders headers, PersistentEntityResourceAssembler assembler, Object source) { - String selfLink = assembler.getSelfLinkFor(source).getHref(); + String selfLink = assembler.getExpandedSelfLink(source).getHref(); headers.setLocation(new UriTemplate(selfLink).expand()); } 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 fc46199d6..f7bd9b9ed 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 @@ -134,11 +134,11 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } else { PersistentEntityResource resource = assembler.toResource(it); - headers.set("Content-Location", resource.getId().getHref()); + headers.set("Content-Location", resource.getRequiredLink(Link.REL_SELF).getHref()); return resource; } - }).orElseThrow(() -> new ResourceNotFoundException()); + }).orElseThrow(ResourceNotFoundException::new); return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, // doWithReferencedProperty(repoRequest, id, property, handler, HttpMethod.GET)); @@ -187,7 +187,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro if (propertyId.equals(accessor1.getIdentifier().toString())) { PersistentEntityResource resource1 = assembler.toResource(obj); - headers.set("Content-Location", resource1.getId().getHref()); + headers.set("Content-Location", resource1.getRequiredLink(Link.REL_SELF).getHref()); return resource1; } } @@ -200,18 +200,18 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro if (propertyId.equals(accessor2.getIdentifier().toString())) { PersistentEntityResource resource2 = assembler.toResource(entry.getValue()); - headers.set("Content-Location", resource2.getId().getHref()); + headers.set("Content-Location", resource2.getRequiredLink(Link.REL_SELF).getHref()); return resource2; } } } else { - return new Resource(prop.propertyValue); + return new Resource<>(prop.propertyValue); } throw new ResourceNotFoundException(); - }).orElseThrow(() -> new ResourceNotFoundException()); + }).orElseThrow(ResourceNotFoundException::new); return ControllerUtils.toResponseEntity(HttpStatus.OK, headers, // doWithReferencedProperty(repoRequest, id, property, handler, HttpMethod.GET)); @@ -254,7 +254,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro Map> map = (Map>) content; for (Entry> entry : map.entrySet()) { - Link l = new Link(entry.getValue().getLink("self").getHref(), entry.getKey().toString()); + Link l = new Link(entry.getValue().getRequiredLink(Link.REL_SELF).getHref(), entry.getKey().toString()); links.add(l); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java index 77608356c..08f73fd7c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java @@ -344,23 +344,20 @@ class RepositorySearchController extends AbstractRepositoryRestController { List> parameterTypeInformations = ClassTypeInformation.from(method.getDeclaringClass()) .getParameterTypes(method); - for (Entry> entry : parameters.entrySet()) { + parameters.entrySet().forEach(entry -> - MethodParameter parameter = methodParameters.getParameter(entry.getKey()); + methodParameters.getParameter(entry.getKey()).ifPresent(parameter -> { - if (parameter == null) { - continue; + int parameterIndex = parameterList.indexOf(parameter); + TypeInformation domainType = parameterTypeInformations.get(parameterIndex).getActualType(); + + ResourceMetadata metadata = mappings.getMetadataFor(domainType.getType()); + + if (metadata != null && metadata.isExported()) { + result.put(parameter.getParameterName(), prepareUris(entry.getValue())); + } } - - int parameterIndex = parameterList.indexOf(parameter); - TypeInformation domainType = parameterTypeInformations.get(parameterIndex).getActualType(); - - ResourceMetadata metadata = mappings.getMetadataFor(domainType.getType()); - - if (metadata != null && metadata.isExported()) { - result.put(parameter.getParameterName(), prepareUris(entry.getValue())); - } - } + )); return invoker.invokeQueryMethod(method, result, pageable.getPageable(), sort); } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java index 91cc50a61..caebcc3fe 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java @@ -122,7 +122,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { descriptors.addAll(buildSearchResourceDescriptors(resourceInformation.getPersistentEntity())); - return Alps.alps().descriptors(descriptors).build(); + return Alps.alps().descriptor(descriptors).build(); } private Descriptor buildRepresentationDescriptor(Class type) { @@ -135,7 +135,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { id(getRepresentationDescriptorId(metadata)).// href(href).// doc(getDocFor(metadata.getItemResourceDescription())).// - descriptors(buildPropertyDescriptors(type, metadata.getItemResourceRel())).// + descriptor(buildPropertyDescriptors(type, metadata.getItemResourceRel())).// build(); } @@ -155,7 +155,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { type(descriptorType).// doc(getDocFor(metadata.getDescription())).// rt("#" + representationDescriptor.getId()).// - descriptors(nestedDescriptors).build(); + descriptor(nestedDescriptors).build(); } /** @@ -184,7 +184,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { type(Type.SEMANTIC).// name(projection.getKey()).// doc(getDocFor(projectionDescription)).// - descriptors(createJacksonDescriptor(projection.getKey(), type)).// + descriptor(createJacksonDescriptor(projection.getKey(), type)).// build()); } @@ -192,7 +192,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { type(Type.SEMANTIC).// name(projectionParameterName).// doc(getDocFor(SimpleResourceDescription.defaultFor(projectionParameterName))).// - descriptors(projectionDescriptors).build(); + descriptor(projectionDescriptors).build(); } private List createJacksonDescriptor(String name, Class type) { @@ -231,7 +231,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { type(getType(method)).// doc(getDocFor(metadata.getItemResourceDescription())).// rt("#".concat(representationDescriptor.getId())). // - descriptors(getProjectionDescriptor(entity.getType(), method)).// + descriptor(getProjectionDescriptor(entity.getType(), method)).// build(); } @@ -375,7 +375,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { descriptors.add(descriptor().// type(Type.SAFE).// name(methodMapping.getRel()).// - descriptors(parameterDescriptors).// + descriptor(parameterDescriptors).// build()); } 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 dc9ea1a85..e9555379f 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 @@ -530,8 +530,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon public ObjectMapper halObjectMapper() { - RelProvider defaultedRelProvider = this.relProvider.orElseGet(() -> new EvoInflectorRelProvider()); - HalConfiguration halConfiguration = this.halConfiguration.orElseGet(() -> new HalConfiguration()); + RelProvider defaultedRelProvider = this.relProvider.orElseGet(EvoInflectorRelProvider::new); + HalConfiguration halConfiguration = this.halConfiguration.orElseGet(HalConfiguration::new); HalHandlerInstantiator instantiator = new HalHandlerInstantiator(defaultedRelProvider, curieProvider.orElse(null), resourceDescriptionMessageSourceAccessor(), halConfiguration); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index 6c4c5ccf8..752a58ac4 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -123,7 +123,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { LookupObjectSerializer lookupObjectSerializer, ResourceProcessorInvoker invoker, EmbeddedResourcesAssembler assembler) { - super(new Version(2, 0, 0, null, "org.springframework.data.rest", "jackson-module")); + super("persistent-entity-resource", new Version(2, 0, 0, null, "org.springframework.data.rest", "jackson-module")); Assert.notNull(associations, "AssociationLinks must not be null!"); Assert.notNull(entities, "Repositories must not be null!"); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/LinkCollector.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/LinkCollector.java index f534d165c..a1dfc82c4 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/LinkCollector.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/LinkCollector.java @@ -141,7 +141,7 @@ public class LinkCollector { private Link createSelfLink(Object object, Links existing) { if (existing.hasLink(Link.REL_SELF)) { - return existing.getLink(Link.REL_SELF); + return existing.getLink(Link.REL_SELF).get(); } return links.createSelfLinkFor(object).withSelfRel();