From 18616d9c89515eda4158c5b1a0a36ddc37c794f1 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Fri, 31 Aug 2012 09:39:41 -0500 Subject: [PATCH] DATAREST-45 Using the wrong base URI to build links to the "self" and properties of entities. Needed to add two lines of code to generate a proper self link and use that as the base URI for generating property links. --- .../data/rest/webmvc/EntityToResourceConverter.java | 8 ++++++-- .../data/rest/webmvc/RepositoryRestController.java | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EntityToResourceConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EntityToResourceConverter.java index 1dc55f8a2..799a81e66 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EntityToResourceConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/EntityToResourceConverter.java @@ -2,6 +2,7 @@ package org.springframework.data.rest.webmvc; import static org.springframework.data.rest.core.util.UriUtils.*; +import java.io.Serializable; import java.net.URI; import java.util.HashMap; import java.util.HashSet; @@ -39,13 +40,16 @@ public class EntityToResourceConverter implements Converter { } URI baseUri = RepositoryRestController.BASE_URI.get(); + Serializable id = (Serializable)repositoryMetadata.entityMetadata().idAttribute().get(source); + URI selfUri = buildUri(baseUri, repositoryMetadata.name(), id.toString()); + Set links = new HashSet(); for(Object attrName : entityMetadata.linkedAttributes().keySet()) { - URI uri = buildUri(baseUri, attrName.toString()); + URI uri = buildUri(selfUri, attrName.toString()); String rel = repositoryMetadata.rel() + "." + source.getClass().getSimpleName() + "." + attrName; links.add(new Link(uri.toString(), rel)); } - links.add(new Link(baseUri.toString(), "self")); + links.add(new Link(selfUri.toString(), "self")); Map entityDto = new HashMap(); for(Map.Entry attrMeta : ((Map)entityMetadata.embeddedAttributes()) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index 43ed64f52..2390280cc 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -469,7 +469,8 @@ public class RepositoryRestController Object o = allEntities.next(); if(shouldReturnLinks(request.getServletRequest().getHeader("Accept"))) { Serializable id = (Serializable)repoMeta.entityMetadata().idAttribute().get(o); - links.add(new Link(buildUri(baseUri, repository, id.toString()).toString(), + URI selfUri = buildUri(baseUri, repository, id.toString()); + links.add(new Link(selfUri.toString(), repoMeta.rel() + "." + o.getClass().getSimpleName())); } else { allResources.add(o);