From 9a77f20bf415fe31e0dd3e27421307eb00d8f52e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 13 Apr 2015 11:39:59 +0200 Subject: [PATCH] DATAREST-517 - Null query results are now translated into 404. We now explicitly handle null query execution results by returning 404 Not Found. --- .../webmvc/AbstractRepositoryRestController.java | 8 +++++--- .../data/rest/webmvc/mongodb/MongoWebTests.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java index 7224f63d2..4f919cda9 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java @@ -100,11 +100,13 @@ class AbstractRepositoryRestController { if (source instanceof Iterable) { return toResources((Iterable) source, assembler, baseLink); - } else if (source == null || ClassUtils.isPrimitiveOrWrapper(source.getClass())) { + } else if (source == null) { + throw new ResourceNotFoundException(); + } else if (ClassUtils.isPrimitiveOrWrapper(source.getClass())) { return source; - } else { - return assembler.toFullResource(source); } + + return assembler.toFullResource(source); } protected Resources> entitiesToResources(Page page, diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java index 2e5b32017..a10414860 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java @@ -315,4 +315,16 @@ public class MongoWebTests extends CommonWebTests { mvc.perform(get(link.expand(profile.getId()).getHref())).// andExpect(status().isOk()); } + + /** + * @see DATAREST-517 + */ + @Test + public void returnsNotFoundIfQueryExecutionDoesNotReturnResult() throws Exception { + + Link link = client.discoverUnique("profiles", "search", "findById"); + + mvc.perform(get(link.expand("").getHref())).// + andExpect(status().isNotFound()); + } }