DATACMNS-630 - Added HandlerMethodArgumentResolver to create proxies for interfaces.
We now ship a ProxyingHandlerMethodArgumentResolver that gets registered when @EnableSpringDataWebSupport is activated. It creates Map-based proxy instances for interfaces used as Spring MVC controller method parameters.
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2015 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.web;
|
||||
|
||||
import static java.util.Collections.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValues;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MapDataBinder}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MapDataBinderUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-630
|
||||
*/
|
||||
@Test
|
||||
public void honorsFormattingAnnotationOnAccessor() {
|
||||
|
||||
Date reference = new Date();
|
||||
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("foo.date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(reference));
|
||||
|
||||
Map<String, Object> nested = new HashMap<String, Object>();
|
||||
nested.put("date", reference);
|
||||
|
||||
assertThat(bind(values), hasEntry("foo", (Object) nested));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-630
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void bindsNestedCollectionElement() {
|
||||
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("foo.bar.fooBar[0]", "String");
|
||||
|
||||
Map<String, Object> result = bind(values);
|
||||
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("String");
|
||||
|
||||
assertThat(result, is((Map) singletonMap("foo", singletonMap("bar", singletonMap("fooBar", list)))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-630
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void bindsNestedPrimitive() {
|
||||
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("foo.firstname", "Dave");
|
||||
values.add("foo.lastname", "Matthews");
|
||||
|
||||
Map<String, Object> result = bind(values);
|
||||
|
||||
Map<String, Object> dave = new HashMap<String, Object>();
|
||||
dave.put("firstname", "Dave");
|
||||
dave.put("lastname", "Matthews");
|
||||
|
||||
assertThat(result, is((Map) singletonMap("foo", dave)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-630
|
||||
*/
|
||||
@Test
|
||||
public void skipsPropertyNotExposedByTheTypeHierarchy() {
|
||||
|
||||
MutablePropertyValues values = new MutablePropertyValues();
|
||||
values.add("somethingWeird", "Value");
|
||||
|
||||
assertThat(bind(values), is(Collections.<String, Object> emptyMap()));
|
||||
}
|
||||
|
||||
private static Map<String, Object> bind(PropertyValues values) {
|
||||
|
||||
MapDataBinder binder = new MapDataBinder(Root.class, new DefaultFormattingConversionService());
|
||||
binder.bind(values);
|
||||
|
||||
return binder.getTarget();
|
||||
}
|
||||
|
||||
interface Root {
|
||||
|
||||
Foo getFoo();
|
||||
|
||||
Bar getBar();
|
||||
}
|
||||
|
||||
interface Foo {
|
||||
|
||||
Bar getBar();
|
||||
|
||||
String getLastname();
|
||||
|
||||
String getFirstname();
|
||||
|
||||
@DateTimeFormat(iso = ISO.DATE_TIME)
|
||||
Date getDate();
|
||||
}
|
||||
|
||||
interface Bar {
|
||||
Collection<String> getFooBar();
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@ package org.springframework.data.web.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
@@ -27,6 +29,7 @@ import org.hamcrest.Matcher;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.geo.Distance;
|
||||
@@ -36,10 +39,14 @@ import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver;
|
||||
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
|
||||
import org.springframework.data.web.WebTestUtils;
|
||||
import org.springframework.data.web.config.EnableSpringDataWebSupport.SpringDataWebConfigurationImportSelector;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link EnableSpringDataWebSupport}.
|
||||
@@ -57,6 +64,9 @@ public class EnableSpringDataWebSupportIntegrationTests {
|
||||
@EnableSpringDataWebSupport
|
||||
static class SampleConfig {
|
||||
|
||||
public @Bean SampleController controller() {
|
||||
return new SampleController();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -141,6 +151,27 @@ public class EnableSpringDataWebSupportIntegrationTests {
|
||||
assertThat(conversionService.canConvert(Point.class, String.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-630
|
||||
*/
|
||||
@Test
|
||||
public void createsProxyForInterfaceBasedControllerMethodParameter() throws Exception {
|
||||
|
||||
WebApplicationContext applicationContext = WebTestUtils.createApplicationContext(SampleConfig.class);
|
||||
MockMvc mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
|
||||
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("/proxy");
|
||||
builder.queryParam("name", "Foo");
|
||||
builder.queryParam("shippingAddresses[0].zipCode", "ZIP");
|
||||
builder.queryParam("shippingAddresses[0].city", "City");
|
||||
builder.queryParam("billingAddress.zipCode", "ZIP");
|
||||
builder.queryParam("billingAddress.city", "City");
|
||||
builder.queryParam("date", "2014-01-11");
|
||||
|
||||
mvc.perform(post(builder.build().toString())).//
|
||||
andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void assertResolversRegistered(ApplicationContext context, Class<?>... resolverTypes) {
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2015 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.web.config;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.data.web.config.SampleController.SampleDto.Address;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Controller
|
||||
public class SampleController {
|
||||
|
||||
@RequestMapping("/proxy")
|
||||
public String someMethod(SampleDto sampleDto) {
|
||||
|
||||
assertThat(sampleDto, is(notNullValue()));
|
||||
assertThat(sampleDto.getName(), is("Foo"));
|
||||
assertThat(sampleDto.getDate(), is(notNullValue()));
|
||||
|
||||
Collection<Address> shippingAddresses = sampleDto.getShippingAddresses();
|
||||
|
||||
assertThat(shippingAddresses, is(hasSize(1)));
|
||||
assertThat(shippingAddresses.iterator().next().getZipCode(), is("ZIP"));
|
||||
assertThat(shippingAddresses.iterator().next().getCity(), is("City"));
|
||||
|
||||
assertThat(sampleDto.getBillingAddress(), is(notNullValue()));
|
||||
assertThat(sampleDto.getBillingAddress().getZipCode(), is("ZIP"));
|
||||
assertThat(sampleDto.getBillingAddress().getCity(), is("City"));
|
||||
|
||||
return "view";
|
||||
}
|
||||
|
||||
interface SampleDto {
|
||||
|
||||
String getName();
|
||||
|
||||
@DateTimeFormat(iso = ISO.DATE)
|
||||
Date getDate();
|
||||
|
||||
Address getBillingAddress();
|
||||
|
||||
Collection<Address> getShippingAddresses();
|
||||
|
||||
interface Address {
|
||||
|
||||
String getZipCode();
|
||||
|
||||
String getCity();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user