From 11668b34392e129c59f6762f9f7a2b09d6d101ae Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 9 Dec 2014 12:56:13 +0100 Subject: [PATCH] DATAREST-423 - Fixed HandlerAdapterSetup to consider ConversionService. The special RepositoryRestHandlerAdapter we set up for repository invocations didn't consider the ConversionService Spring Data REST sets up. This causes binding issues in case someone wants to override resource handling by declaring an @RepositoryRestController and using a domain type as handler method argument. We now explicitly configure the RepositoryRestHandlerAdapter to use the ConversionService we set up and equip with Spring Data Commons' DomainClassConverter. --- .../RepositoryRestMvcConfiguration.java | 6 +++ .../rest/webmvc/jpa/AuthorsController.java | 41 +++++++++++++++++++ .../data/rest/webmvc/jpa/JpaWebTests.java | 18 +++++++- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/AuthorsController.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 4f899cf22..e4125998a 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -114,6 +114,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert import org.springframework.plugin.core.OrderAwarePluginRegistry; import org.springframework.plugin.core.PluginRegistry; import org.springframework.util.ClassUtils; +import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @@ -478,8 +479,13 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon AnnotationAwareOrderComparator.sort(processors); + // Forward conversion service to handler adapter + ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); + initializer.setConversionService(defaultConversionService()); + RepositoryRestHandlerAdapter handlerAdapter = new RepositoryRestHandlerAdapter(defaultMethodArgumentResolvers(), processors); + handlerAdapter.setWebBindingInitializer(initializer); handlerAdapter.setMessageConverters(messageConverters); return handlerAdapter; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/AuthorsController.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/AuthorsController.java new file mode 100644 index 000000000..5f425c79d --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/AuthorsController.java @@ -0,0 +1,41 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.jpa; + +import org.springframework.data.rest.webmvc.RepositoryRestController; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Sample custom controller to test the ability to override + * + * @author Oliver Gierke + */ +@RepositoryRestController +public class AuthorsController { + + @RequestMapping(value = "/authors/{author}", method = RequestMethod.DELETE) + HttpEntity deleteAuthor(@PathVariable Author author) { + + Assert.notNull(author, "Author must not be null!"); + return new ResponseEntity(HttpStatus.I_AM_A_TEAPOT); + } +} 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 ebe075c71..437fca51e 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 @@ -551,9 +551,9 @@ public class JpaWebTests extends CommonWebTests { @Test public void returns404WhenTryingToDeleteANonExistingResource() throws Exception { - Link authorsLink = client.discoverUnique("authors"); + Link peopleLink = client.discoverUnique("people"); - mvc.perform(delete(authorsLink.getHref().concat("/{id}"), 4711)).// + mvc.perform(delete(peopleLink.expand().getHref().concat("/{id}"), 4711)).// andExpect(status().isNotFound()); } @@ -581,6 +581,20 @@ public class JpaWebTests extends CommonWebTests { andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data (Second Edition)")); } + /** + * @see DATAREST-423 + */ + @Test + public void invokesCustomControllerAndBindsDomainObjectCorrectly() throws Exception { + + MockHttpServletResponse authorsResponse = client.request(client.discoverUnique("authors")); + + String authorUri = JsonPath.read(authorsResponse.getContentAsString(), "$._embedded.authors[0]._links.self.href"); + + mvc.perform(delete(authorUri)).// + andExpect(status().isIAmATeapot()); + } + /** * Asserts the {@link Person} resource the given link points to contains siblings with the given names. *