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 81271828b..b670ad92a 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 @@ -360,6 +360,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 d7ff222b7..8abb0f6b6 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 @@ -545,6 +545,18 @@ public class JpaWebTests extends AbstractWebIntegrationTests { follow(new Link(href)).andExpect(hasLinkWithRel("books")); } + /** + * @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. *