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 60d06af14..43ed64f52 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 @@ -337,7 +337,7 @@ public class RepositoryRestController entityConverters.addConverter(domainType, Resource.class, new EntityToResourceConverter(repoMeta)); } } - conversionService.addConversionService(0, entityConverters); + conversionService.addConversionServices(entityConverters); } /** @@ -642,7 +642,7 @@ public class RepositoryRestController new Resources(Collections.emptyList())); } - Set links = new HashSet(); + Set links = new HashSet(); PagedResources.PageMetadata pageMetadata = null; Iterator entities = Collections.emptyList().iterator(); @@ -708,7 +708,7 @@ public class RepositoryRestController String id = elemRepoMeta.entityMetadata().idAttribute().get(obj).toString(); String rel = elemRepoMeta.rel() + "." + elemRepoMeta.entityMetadata().type().getSimpleName(); URI path = buildUri(baseUri, repository, id); - links.add(new org.springframework.hateoas.Link(path.toString(), rel)); + links.add(new Link(path.toString(), rel)); } else { results.add(obj); } @@ -718,8 +718,8 @@ public class RepositoryRestController HttpStatus.OK, new HttpHeaders(), (null != pageMetadata - ? PagedResources.wrap(results, pageMetadata) - : Resources.wrap(results))); + ? new PagedResources(results, pageMetadata) + : new Resources(results))); } /** @@ -915,7 +915,7 @@ public class RepositoryRestController Object body = null; if(returnBody(request)) { - body = EntityResource.wrap(savedEntity, repoMeta, selfUri); + body = savedEntity; } if(!isUpdate) { @@ -1075,6 +1075,10 @@ public class RepositoryRestController propertyRel += "." + propRepoMeta.entityMetadata().type().getSimpleName(); Map resource = new HashMap(); for(Map.Entry entry : ((Map)propVal).entrySet()) { + if(null == entry.getValue()) { + continue; + } + String propValId = idAttr.get(entry.getValue()).toString(); URI path = buildUri(baseUri, repository, id, property, propValId); @@ -1102,7 +1106,6 @@ public class RepositoryRestController } else { URI path = buildUri(baseUri, repository, id, property); - if(shouldReturnLinks(accept)) { links.add(new Link(path.toString(), propertyRel)); @@ -1389,13 +1392,18 @@ public class RepositoryRestController URI propertyPath = buildUri(baseUri, repository, id, property, linkedId); URI selfUri = buildUri(baseUri, linkedRepoMeta.name(), linkedId); - EntityResource er = EntityResource.wrap(linkedEntity, linkedRepoMeta, selfUri); - er.add(new Link(propertyPath.toString(), propertyRel)); + Resource r; + if(conversionService.canConvert(linkedEntity.getClass(), Resource.class)) { + r = conversionService.convert(linkedEntity, Resource.class); + } else { + r = new Resource(linkedEntity); + } + r.add(new Link(propertyPath.toString(), propertyRel)); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Location", selfUri.toString()); - return negotiateResponse(request, HttpStatus.OK, headers, er); + return negotiateResponse(request, HttpStatus.OK, headers, r); } /** diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/RepositoryAwareJacksonModule.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/RepositoryAwareJacksonModule.java index 006fad04c..58a2af433 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/RepositoryAwareJacksonModule.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/RepositoryAwareJacksonModule.java @@ -149,9 +149,17 @@ public class RepositoryAwareJacksonModule extends SimpleModule implements Initia return; } - if(conversionService.canConvert(resource.getContent().getClass(), Resource.class)) { + Class sourceType = resource.getContent().getClass(); + ConversionService entityConversionSvc = conversionService; + for(ConversionService cs : conversionServices) { + if(cs.canConvert(sourceType, Resource.class)) { + entityConversionSvc = cs; + break; + } + } + if(entityConversionSvc.canConvert(sourceType, Resource.class)) { Set links = resource.getLinks(); - resource = conversionService.convert(value, Resource.class); + resource = entityConversionSvc.convert(value, Resource.class); resource.add(links); } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java index 9db5b4bd8..584698cb6 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/ApplicationConfig.java @@ -1,7 +1,9 @@ package org.springframework.data.rest.test.webmvc; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; @@ -72,6 +74,7 @@ public class ApplicationConfig { return new TestRepositoryEventListener(); } + @SuppressWarnings({"unchecked"}) @Bean public ConversionService customConversionService() { DefaultFormattingConversionService cs = new DefaultFormattingConversionService(); cs.addConverter(new Converter>() { @@ -83,6 +86,15 @@ public class ApplicationConfig { return longs; } }); + // cs.addConverter(new Converter() { + // @Override public Resource convert(Person person) { + // Map m = new HashMap(); + // m.put("name", person.getName()); + // CustomResource r = new CustomResource(m); + // r.add(new Link("http://localhost:8080/people/1", "self")); + // return r; + // } + // }); return cs; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/CustomResource.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/CustomResource.java new file mode 100644 index 000000000..3d6fc5f31 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/CustomResource.java @@ -0,0 +1,35 @@ +package org.springframework.data.rest.test.webmvc; + +import java.util.Map; +import java.util.Set; + +import org.codehaus.jackson.annotate.JsonAnyGetter; +import org.codehaus.jackson.annotate.JsonProperty; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resource; + +/** + * @author Jon Brisbin + */ +public class CustomResource extends Resource> { + + public CustomResource(Map properties) { + super(properties); + } + + @JsonProperty("@id") + public String getSelfLink() { + return super.getId().getHref(); + } + + @JsonProperty("_links") + @Override public Set getLinks() { + return super.getLinks(); + } + + @JsonAnyGetter + @Override public Map getContent() { + return super.getContent(); + } + +}