diff --git a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/config/RepositoryRestConfiguration.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/config/RepositoryRestConfiguration.java index 8d5a963d9..ca50c3da5 100644 --- a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/config/RepositoryRestConfiguration.java +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/config/RepositoryRestConfiguration.java @@ -27,6 +27,8 @@ public class RepositoryRestConfiguration { private Map, Class> typeMappings = Collections.emptyMap(); private MediaType defaultMediaType = MediaType.APPLICATION_JSON; private boolean dumpErrors = true; + private boolean returnBodyOnCreate = false; + private boolean returnBodyOnUpdate = false; private List> exposeIdsFor = new ArrayList>(); private ResourceMappingConfiguration domainMappings = new ResourceMappingConfiguration(); private ResourceMappingConfiguration repoMappings = new ResourceMappingConfiguration(); @@ -309,6 +311,49 @@ public class RepositoryRestConfiguration { return this; } + /** + * Whether to return a response body after creating an entity. + * + * @return {@literal true} to return a body on create, {@literal false} otherwise. + */ + public boolean isReturnBodyOnCreate() { + return returnBodyOnCreate; + } + + /** + * Set whether to return a response body after creating an entity. + * + * @param returnBodyOnCreate + * {@literal true} to return a body on create, {@literal false} otherwise. + * + * @return {@literal this} + */ + public RepositoryRestConfiguration setReturnBodyOnCreate(boolean returnBodyOnCreate) { + this.returnBodyOnCreate = returnBodyOnCreate; + return this; + } + + /** + * Whether to return a response body after updating an entity. + * + * @return {@literal true} to return a body on update, {@literal false} otherwise. + */ + public boolean isReturnBodyOnUpdate() { + return returnBodyOnUpdate; + } + + /** + * Sets whether to return a response body after updating an entity. + * + * @param returnBodyOnUpdate + * + * @return + */ + public RepositoryRestConfiguration setReturnBodyOnUpdate(boolean returnBodyOnUpdate) { + this.returnBodyOnUpdate = returnBodyOnUpdate; + return this; + } + /** * Start configuration a {@link ResourceMapping} for a specific domain type. * @@ -333,10 +378,23 @@ public class RepositoryRestConfiguration { return domainMappings.getResourceMappingFor(domainType); } + /** + * Whether there is a {@link ResourceMapping} for the given domain type. + * + * @param domainType + * The domain type to find a {@link ResourceMapping} for. + * + * @return {@literal true} if a {@link ResourceMapping} exists for this domain class, {@literal false} otherwise. + */ public boolean hasResourceMappingForDomainType(Class domainType) { return domainMappings.hasResourceMappingFor(domainType); } + /** + * Get the {@link ResourceMappingConfiguration} that is currently configured. + * + * @return + */ public ResourceMappingConfiguration getDomainTypesResourceMappingConfiguration() { return domainMappings; } @@ -365,6 +423,13 @@ public class RepositoryRestConfiguration { return repoMappings.getResourceMappingFor(repositoryInterface); } + /** + * Whether there is a {@link ResourceMapping} configured for this {@literal Repository} class. + * + * @param repositoryInterface + * + * @return + */ public boolean hasResourceMappingForRepository(Class repositoryInterface) { return repoMappings.hasResourceMappingFor(repositoryInterface); } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java index 040b6a77e..aab4cb9a4 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java @@ -178,12 +178,18 @@ public class RepositoryEntityController extends AbstractRepositoryRestController HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(selfLink.getHref())); - return resourceResponse(headers, - new PersistentEntityResource(repoRequest.getPersistentEntity(), - obj, - selfLink) - .setBaseUri(repoRequest.getBaseUri()), - HttpStatus.CREATED); + if(config.isReturnBodyOnCreate()) { + return resourceResponse(headers, + new PersistentEntityResource(repoRequest.getPersistentEntity(), + obj, + selfLink) + .setBaseUri(repoRequest.getBaseUri()), + HttpStatus.CREATED); + } else { + return resourceResponse(headers, + null, + HttpStatus.CREATED); + } } @SuppressWarnings({"unchecked"}) @@ -207,7 +213,9 @@ public class RepositoryEntityController extends AbstractRepositoryRestController value = "/{id}", method = RequestMethod.GET, produces = { - "application/json" + "application/json", + "application/x-spring-data-compact+json", + "text/uri-list" } ) @ResponseBody @@ -290,13 +298,19 @@ public class RepositoryEntityController extends AbstractRepositoryRestController Object obj = repoMethodInvoker.save(domainObj); applicationContext.publishEvent(new AfterSaveEvent(obj)); - PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), - obj, - repoRequest.getBaseUri()); - per.add(repoRequest.buildEntitySelfLink(obj, conversionService)); - return resourceResponse(null, - per, - HttpStatus.OK); + if(config.isReturnBodyOnUpdate()) { + PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), + obj, + repoRequest.getBaseUri()); + per.add(repoRequest.buildEntitySelfLink(obj, conversionService)); + return resourceResponse(null, + per, + HttpStatus.OK); + } else { + return resourceResponse(null, + null, + HttpStatus.NO_CONTENT); + } } @SuppressWarnings({"unchecked"}) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java index 1268bcc0d..0b7eee5a4 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryPropertyReferenceController.java @@ -1,6 +1,7 @@ package org.springframework.data.rest.webmvc; import static org.springframework.data.rest.core.util.UriUtils.*; +import static org.springframework.data.rest.repository.support.ResourceMappingUtils.*; import java.net.URI; import java.util.ArrayList; @@ -119,7 +120,9 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes method = RequestMethod.GET, produces = { "application/json", - "application/x-spring-data-verbose+json" + "application/x-spring-data-verbose+json", + "application/x-spring-data-compact+json", + "text/uri-list" } ) @ResponseBody @@ -141,7 +144,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes Link selfLink = repoRequest.buildEntitySelfLink(obj, conversionService); per.add(selfLink); headers.set("Content-Location", selfLink.getHref()); - return new Resource(per); + return per; } } } else if(prop.property.isMap()) { @@ -156,7 +159,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes Link selfLink = repoRequest.buildEntitySelfLink(entry.getValue(), conversionService); per.add(selfLink); headers.set("Content-Location", selfLink.getHref()); - return new Resource(per, selfLink); + return per; } } } else { @@ -192,8 +195,18 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes ResourceMapping repoMapping = repoRequest.getRepositoryResourceMapping(); ResourceMapping entityMapping = repoRequest.getPersistentEntityResourceMapping(); + String propName = entityMapping.getNameForPath(property); ResourceMapping propMapping = entityMapping.getResourceMappingFor(entityMapping.getNameForPath(property)); - String propRel = (null != propMapping ? propMapping.getRel() : property); + PersistentProperty persistentProp = repoRequest.getPersistentEntity().getPersistentProperty(propName); + Class propType = (persistentProp.isCollectionLike() || persistentProp.isMap() + ? persistentProp.getComponentType() + : persistentProp.getType()); + ResourceMapping propRepoMapping = getResourceMapping(config, repositories.getRepositoryInformationFor(propType)); + String propRel = String.format("%s.%s.%s.%s", + repoMapping.getRel(), + entityMapping.getRel(), + (null != propMapping ? propMapping.getRel() : property), + propRepoMapping.getRel()); Resource resource = response.getBody(); @@ -330,7 +343,7 @@ public class RepositoryPropertyReferenceController extends AbstractRepositoryRes id, property, handler); - return resourceResponse(null, EMPTY_RESOURCE, HttpStatus.NO_CONTENT); + return resourceResponse(null, EMPTY_RESOURCE, HttpStatus.CREATED); } @SuppressWarnings({"unchecked"})