Tweaking representations and return values to get the right nesting of resources.

This commit is contained in:
Jon Brisbin
2012-08-31 09:26:28 -05:00
committed by Jon Brisbin
parent 1fdde3531e
commit e4d43278f7
4 changed files with 75 additions and 12 deletions

View File

@@ -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<String>(Collections.<String>emptyList()));
}
Set<org.springframework.hateoas.Link> links = new HashSet<org.springframework.hateoas.Link>();
Set<Link> links = new HashSet<Link>();
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<String, Object> resource = new HashMap<String, Object>();
for(Map.Entry<Object, Object> entry : ((Map<Object, Object>)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<Object>(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);
}
/**

View File

@@ -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<Link> links = resource.getLinks();
resource = conversionService.convert(value, Resource.class);
resource = entityConversionSvc.convert(value, Resource.class);
resource.add(links);
}

View File

@@ -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<String[], List<Long>>() {
@@ -83,6 +86,15 @@ public class ApplicationConfig {
return longs;
}
});
// cs.addConverter(new Converter<Person, Resource>() {
// @Override public Resource convert(Person person) {
// Map<String, Object> m = new HashMap<String, Object>();
// m.put("name", person.getName());
// CustomResource r = new CustomResource(m);
// r.add(new Link("http://localhost:8080/people/1", "self"));
// return r;
// }
// });
return cs;
}

View File

@@ -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<Map<String, Object>> {
public CustomResource(Map<String, Object> properties) {
super(properties);
}
@JsonProperty("@id")
public String getSelfLink() {
return super.getId().getHref();
}
@JsonProperty("_links")
@Override public Set<Link> getLinks() {
return super.getLinks();
}
@JsonAnyGetter
@Override public Map<String, Object> getContent() {
return super.getContent();
}
}