From b458378bbc7614c828dc61880c7c97ac1a5688a6 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 2 May 2014 11:19:44 +0200 Subject: [PATCH] DATAREST-276 - Yet another round of trailing slash handling. We need to proactively strip trailing slashes during base URI handling as even on a successful match (which previously left trailing slashes intact) this might subsequently on the resolving of the repository metadata when evaluating the left-over lookup path against the @RequestMapping methods (which keeps trailing slashes). --- .../org/springframework/data/rest/webmvc/BaseUri.java | 2 ++ .../ResourceMetadataHandlerMethodArgumentResolver.java | 2 +- .../data/rest/webmvc/BaseUriUnitTests.java | 8 ++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BaseUri.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BaseUri.java index 2aaaedf5e..58cfd4e05 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BaseUri.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BaseUri.java @@ -103,6 +103,8 @@ public class BaseUri { Assert.notNull(lookupPath, "Lookup path must not be null!"); + lookupPath = trimTrailingCharacter(lookupPath, '/'); + if (!baseUri.isAbsolute()) { String uri = baseUri.toString(); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ResourceMetadataHandlerMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ResourceMetadataHandlerMethodArgumentResolver.java index 3ad208be6..bd0b91847 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ResourceMetadataHandlerMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ResourceMetadataHandlerMethodArgumentResolver.java @@ -94,6 +94,6 @@ public class ResourceMetadataHandlerMethodArgumentResolver implements HandlerMet } } - return null; + throw new IllegalArgumentException(String.format("Could not resolve repository metadata for %s.", repositoryKey)); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/BaseUriUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/BaseUriUnitTests.java index d36c6953d..5ec028625 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/BaseUriUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/BaseUriUnitTests.java @@ -59,7 +59,7 @@ public class BaseUriUnitTests { BaseUri uri = new BaseUri(URI.create("foo/")); assertThat(uri.getRepositoryLookupPath("/foo"), isEmptyString()); - assertThat(uri.getRepositoryLookupPath("/foo/"), is("/")); + assertThat(uri.getRepositoryLookupPath("/foo/"), isEmptyString()); } /** @@ -71,7 +71,7 @@ public class BaseUriUnitTests { BaseUri uri = new BaseUri(URI.create("/foo")); assertThat(uri.getRepositoryLookupPath("/foo"), isEmptyString()); - assertThat(uri.getRepositoryLookupPath("/foo/"), is("/")); + assertThat(uri.getRepositoryLookupPath("/foo/"), isEmptyString()); } /** @@ -83,8 +83,8 @@ public class BaseUriUnitTests { BaseUri uri = new BaseUri(URI.create("http://localhost:8080/foo/")); assertThat(uri.getRepositoryLookupPath("/foo"), isEmptyString()); - assertThat(uri.getRepositoryLookupPath("/foo/"), is("/")); + assertThat(uri.getRepositoryLookupPath("/foo/"), isEmptyString()); assertThat(uri.getRepositoryLookupPath("/foo/people"), is("/people")); - assertThat(uri.getRepositoryLookupPath("/foo/people/"), is("/people/")); + assertThat(uri.getRepositoryLookupPath("/foo/people/"), is("/people")); } }