Skip to content

Commit 1b5557e

Browse files
committed
DATAREST-1205 - Use Affordances API
Leverage the Affordances API while making other media types work.
1 parent 6986a35 commit 1b5557e

File tree

20 files changed

+499
-54
lines changed

20 files changed

+499
-54
lines changed

spring-data-rest-core/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
</parent>
1717

1818
<properties>
19-
<springplugin>1.2.0.RELEASE</springplugin>
19+
<springplugin>2.0.0.BUILD-SNAPSHOT</springplugin>
2020
<evoinflector>1.2.2</evoinflector>
21+
<spring-hateoas>1.0.0.SDR-SNAPSHOT</spring-hateoas>
2122
<java-module-name>spring.data.rest.core</java-module-name>
2223
<project.root>${basedir}/..</project.root>
2324
</properties>

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static <T extends Plugin<S>, S> Java8PluginRegistry<T, S> empty() {
4646
}
4747

4848
public Optional<T> getPluginFor(S delimiter) {
49-
return Optional.ofNullable(registry.getPluginFor(delimiter));
49+
return registry.getPluginFor(delimiter);
5050
}
5151

5252
public T getPluginOrDefaultFor(S delimiter, T fallback) {

spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Link assertHasLinkEndingWith(String rel, String hrefEnd) {
8383

8484
private final Link assertHasLinkMatching(String rel, Matcher<String> hrefMatcher) {
8585

86-
Link link = resource.getLink(rel);
86+
Link link = resource.getRequiredLink(rel);
8787
assertThat("Expected link with rel '" + rel + "' but didn't find it in " + resource.getLinks(), link,
8888
is(notNullValue()));
8989

spring-data-rest-tests/spring-data-rest-tests-jpa/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
<scope>test</scope>
4848
</dependency>
4949

50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-web</artifactId>
53+
<version>${spring}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.springframework.hateoas</groupId>
58+
<artifactId>spring-hateoas</artifactId>
59+
<version>1.0.0.SDR-SNAPSHOT</version>
60+
<scope>test</scope>
61+
</dependency>
62+
5063
<!-- Jackson Hibernate -->
5164

5265
<dependency>

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/AffordanceIntegrationTests.java

Lines changed: 271 additions & 0 deletions
Large diffs are not rendered by default.

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void returnsLinksToSearchResources() {
9999

100100
assertThat(links.hasLink("firstname")).isTrue();
101101

102-
Link firstnameLink = links.getLink("firstname");
102+
Link firstnameLink = links.getLink("firstname").orElse(null);
103103
assertThat(firstnameLink.isTemplated()).isTrue();
104104
assertThat(firstnameLink.getVariableNames()).contains("page", "size");
105105
}

spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void addsSelfAndSingleResourceLinkToResourceByDefault() throws Exception
7272
Links links = new Links(resource.getLinks());
7373

7474
assertThat(links).hasSize(2);
75-
assertThat(links.getLink("self").getVariables()).isEmpty();
76-
assertThat(links.getLink("user").getVariableNames()).contains("projection");
75+
assertThat(links.getLink("self").orElseThrow(() -> new RuntimeException("Unable to find 'self' link")).getVariables()).isEmpty();
76+
assertThat(links.getLink("user").orElseThrow(() -> new RuntimeException("Unable to find 'user' link")).getVariableNames()).contains("projection");
7777
}
7878
}

spring-data-rest-webmvc/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<scope>test</scope>
125125
</dependency>
126126

127+
<dependency>
128+
<groupId>org.springframework.data</groupId>
129+
<artifactId>spring-data-jpa</artifactId>
130+
<version>${springdata.jpa}</version>
131+
<scope>test</scope>
132+
</dependency>
133+
127134
</dependencies>
128135

129136
</project>

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected Link resourceLink(RootResourceInformation resourceLink, Resource resou
6363

6464
ResourceMetadata repoMapping = resourceLink.getResourceMetadata();
6565

66-
Link selfLink = resource.getLink("self");
66+
Link selfLink = resource.getRequiredLink(Link.REL_SELF);
6767
String rel = repoMapping.getItemResourceRel();
6868

6969
return new Link(selfLink.getHref(), rel);

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@
1818
import lombok.NonNull;
1919
import lombok.RequiredArgsConstructor;
2020

21+
import java.util.Optional;
22+
23+
import org.springframework.core.ResolvableType;
2124
import org.springframework.data.mapping.PersistentEntity;
2225
import org.springframework.data.mapping.context.PersistentEntities;
2326
import org.springframework.data.rest.core.support.SelfLinkProvider;
2427
import org.springframework.data.rest.webmvc.PersistentEntityResource.Builder;
2528
import org.springframework.data.rest.webmvc.mapping.Associations;
2629
import org.springframework.data.rest.webmvc.support.Projector;
30+
import org.springframework.hateoas.Affordance;
2731
import org.springframework.hateoas.Link;
2832
import org.springframework.hateoas.ResourceAssembler;
33+
import org.springframework.hateoas.collectionjson.CollectionJsonAffordanceModel;
2934
import org.springframework.hateoas.core.EmbeddedWrapper;
3035
import org.springframework.hateoas.core.EmbeddedWrappers;
36+
import org.springframework.hateoas.hal.forms.HalFormsAffordanceModel;
37+
import org.springframework.http.HttpMethod;
3138
import org.springframework.util.Assert;
3239

3340
/**
@@ -71,10 +78,22 @@ private Builder wrap(Object instance, Object source) {
7178

7279
PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(source.getClass());
7380

81+
Link selfLink = getSelfLinkFor(source);
82+
83+
Affordance putItemAffordance = new Affordance(HttpMethod.PUT, "put" + source.getClass().getName(), ResolvableType.forClass(source.getClass()), null);
84+
putItemAffordance.addAffordanceModel(new HalFormsAffordanceModel(putItemAffordance, selfLink));
85+
putItemAffordance.addAffordanceModel(new CollectionJsonAffordanceModel(putItemAffordance, selfLink));
86+
87+
Affordance patchItemAffordance = new Affordance(HttpMethod.PATCH, "patch" + source.getClass().getName(), ResolvableType.forClass(source.getClass()), null);
88+
patchItemAffordance.addAffordanceModel(new HalFormsAffordanceModel(patchItemAffordance, selfLink));
89+
patchItemAffordance.addAffordanceModel(new CollectionJsonAffordanceModel(patchItemAffordance, selfLink));
90+
7491
return PersistentEntityResource.build(instance, entity).//
75-
withEmbedded(getEmbeddedResources(source)).//
76-
withLink(getSelfLinkFor(source)).//
77-
withLink(linkProvider.createSelfLinkFor(source));
92+
withEmbedded(getEmbeddedResources(source)).//
93+
withLink(selfLink
94+
.andAffordance(putItemAffordance)
95+
.andAffordance(patchItemAffordance)).//
96+
withLink(linkProvider.createSelfLinkFor(source));
7897
}
7998

8099
/**

0 commit comments

Comments
 (0)