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.
This commit is contained in:
Oliver Gierke
2014-12-09 12:56:13 +01:00
parent 11d02e12bc
commit 7c43512d1d
3 changed files with 63 additions and 2 deletions

View File

@@ -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;
@@ -483,8 +484,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;

View File

@@ -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<Object>(HttpStatus.I_AM_A_TEAPOT);
}
}

View File

@@ -552,9 +552,9 @@ public class JpaWebTests extends CommonWebTests {
@Test
public void returns404WhenTryingToDeleteANonExistingResource() throws Exception {
Link authorsLink = client.discoverUnique("authors");
Link receiptsLink = client.discoverUnique("receipts");
mvc.perform(delete(authorsLink.getHref().concat("/{id}"), 4711)).//
mvc.perform(delete(receiptsLink.getHref().concat("/{id}"), 4711)).//
andExpect(status().isNotFound());
}
@@ -614,6 +614,20 @@ public class JpaWebTests extends CommonWebTests {
status().isPreconditionFailed());
}
/**
* @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.
*