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 4f3932dc8..f43e62142 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 @@ -326,6 +326,10 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem RepositoryInvoker invoker = resourceInformation.getInvoker(); Object domainObj = invoker.invokeFindOne(id); + if (domainObj == null) { + throw new ResourceNotFoundException(); + } + publisher.publishEvent(new BeforeDeleteEvent(domainObj)); invoker.invokeDelete(id); publisher.publishEvent(new AfterDeleteEvent(domainObj)); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index a90669b37..400bfccd0 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -524,6 +524,18 @@ public class JpaWebTests extends AbstractWebIntegrationTests { assertThat(JsonPath. read(responseBody, "$.content"), hasSize(0)); } + /** + * @see DATAREST-353 + */ + @Test + public void returns404WhenTryingToDeleteANonExistingResource() throws Exception { + + Link authorsLink = discoverUnique("authors"); + + mvc.perform(delete(authorsLink.getHref().concat("/{id}"), 4711)).// + andExpect(status().isNotFound()); + } + /** * Asserts the {@link Person} resource the given link points to contains siblings with the given names. *