DATAREST-1341 - Upgrade to Spring HATEOAS 1.0.

This commit is contained in:
Greg Turnquist
2018-02-26 11:53:22 -06:00
committed by Oliver Drotbohm
parent a05d6a2a90
commit af6f55a92c
16 changed files with 42 additions and 48 deletions

View File

@@ -35,7 +35,6 @@
<springdata.solr>4.1.0.BUILD-SNAPSHOT</springdata.solr>
<springdata.cassandra>2.2.0.BUILD-SNAPSHOT</springdata.cassandra>
<springdata.keyvalue>2.2.0.BUILD-SNAPSHOT</springdata.keyvalue>
<spring-hateoas>0.25.0.RELEASE</spring-hateoas>
<hibernate.version>5.2.17.Final</hibernate.version>
<jsonpath>1.1.0</jsonpath>

View File

@@ -16,7 +16,7 @@
</parent>
<properties>
<springplugin>1.2.0.RELEASE</springplugin>
<springplugin>2.0.0.BUILD-SNAPSHOT</springplugin>
<evoinflector>1.2.2</evoinflector>
<java-module-name>spring.data.rest.core</java-module-name>
<project.root>${basedir}/..</project.root>

View File

@@ -46,7 +46,7 @@ public class Java8PluginRegistry<T extends Plugin<S>, S> {
}
public Optional<T> getPluginFor(S delimiter) {
return Optional.ofNullable(registry.getPluginFor(delimiter));
return registry.getPluginFor(delimiter);
}
public T getPluginOrDefaultFor(S delimiter, T fallback) {

View File

@@ -83,7 +83,7 @@ public class ResourceTester {
private final Link assertHasLinkMatching(String rel, Matcher<String> 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()));

View File

@@ -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");
}

View File

@@ -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");
}
}

View File

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

View File

@@ -73,7 +73,7 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler<Obje
return PersistentEntityResource.build(instance, entity).//
withEmbedded(getEmbeddedResources(source)).//
withLink(getSelfLinkFor(source)).//
withLink(getExpandedSelfLink(source)).//
withLink(linkProvider.createSelfLinkFor(source));
}
@@ -89,14 +89,12 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler<Obje
}
/**
* Creates the self link for the given domain instance.
* Creates the self link for the given domain instance, with no templated parameters.
*
* @param instance must be a managed entity, not {@literal null}.
* @return
*/
public Link getSelfLinkFor(Object instance) {
Link link = linkProvider.createSelfLinkFor(instance);
return new Link(link.expand().getHref(), Link.REL_SELF);
Link getExpandedSelfLink(Object instance) {
return linkProvider.createSelfLinkFor(instance).withSelfRel().expand();
}
}

View File

@@ -113,6 +113,6 @@ public class RepositoryController extends AbstractRepositoryRestController {
}
}
return new ResponseEntity<RepositoryLinksResource>(resource, HttpStatus.OK);
return new ResponseEntity<>(resource, HttpStatus.OK);
}
}

View File

@@ -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());
}

View File

@@ -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<Object>(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<Object, Resource<?>> map = (Map<Object, Resource<?>>) content;
for (Entry<Object, Resource<?>> 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);
}
}

View File

@@ -344,23 +344,20 @@ class RepositorySearchController extends AbstractRepositoryRestController {
List<TypeInformation<?>> parameterTypeInformations = ClassTypeInformation.from(method.getDeclaringClass())
.getParameterTypes(method);
for (Entry<String, List<Object>> 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);
}

View File

@@ -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<Descriptor> 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());
}

View File

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

View File

@@ -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!");

View File

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