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 ab33ef1df..4751fbd38 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 @@ -21,6 +21,7 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -295,10 +296,11 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro consumes = { "application/json", "application/x-spring-data-compact+json", "text/uri-list" }) @ResponseBody public ResponseEntity createPropertyReference( - final RootResourceInformation resourceInformation, final HttpMethod requestMethod, - final @RequestBody Resources incoming, @BackendId Serializable id, @PathVariable String property) + final RootResourceInformation resourceInformation, final HttpMethod requestMethod, final @RequestBody( + required = false) Resources incoming, @BackendId Serializable id, @PathVariable String property) throws Exception { + final Resources source = incoming == null ? new Resources(Collections.emptyList()) : incoming; final RepositoryInvoker invoker = resourceInformation.getInvoker(); Function handler = new Function() { @@ -318,7 +320,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } // Add to the existing collection - for (Link l : incoming.getLinks()) { + for (Link l : source.getLinks()) { Object propVal = loadPropertyValue(prop.propertyType, l); coll.add(propVal); } @@ -335,7 +337,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro } // Add to the existing collection - for (Link l : incoming.getLinks()) { + for (Link l : source.getLinks()) { Object propVal = loadPropertyValue(prop.propertyType, l); m.put(l.getRel(), propVal); } @@ -349,12 +351,12 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro "Cannot PATCH a reference to this singular property since the property type is not a List or a Map."); } - if (incoming.getLinks().size() != 1) { + if (source.getLinks().size() != 1) { throw new IllegalArgumentException( "Must send only 1 link to update a property reference that isn't a List or a Map."); } - Object propVal = loadPropertyValue(prop.propertyType, incoming.getLinks().get(0)); + Object propVal = loadPropertyValue(prop.propertyType, source.getLinks().get(0)); prop.accessor.setProperty(prop.property, propVal); }