PathMatching options:configure all HandlerMappings
Since SPR-11486 and SPR-10163, Path Matching options can be configured to customize path matching options for RequestMappingHandlerMapping. Prior to this commit, the defined pathMatcher and pathHelper instances were only used in RequestMappingHandlerMapping. This commit now registers pathMatcher and pathHelper beans under well-known names and share them with several HandlerMappings beans, such as ViewControllerMappings and ResourcesMappings. Issue: SPR-11753
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -76,6 +76,7 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
|
||||
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -86,6 +87,8 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class MvcNamespaceTests {
|
||||
|
||||
public static final String VIEWCONTROLLER_BEAN_NAME = "org.springframework.web.servlet.config.viewControllerHandlerMapping";
|
||||
|
||||
private GenericWebApplicationContext appContext;
|
||||
|
||||
private TestController handler;
|
||||
@@ -250,7 +253,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testResources() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-resources.xml", 5);
|
||||
loadBeanDefinitions("mvc-config-resources.xml", 7);
|
||||
|
||||
HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
@@ -283,7 +286,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testResourcesWithOptionalAttributes() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-resources-optional-attrs.xml", 5);
|
||||
loadBeanDefinitions("mvc-config-resources-optional-attrs.xml", 7);
|
||||
|
||||
SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
@@ -365,7 +368,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testViewControllers() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-controllers.xml", 16);
|
||||
loadBeanDefinitions("mvc-config-view-controllers.xml", 18);
|
||||
|
||||
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
@@ -425,7 +428,7 @@ public class MvcNamespaceTests {
|
||||
/** WebSphere gives trailing servlet path slashes by default!! */
|
||||
@Test
|
||||
public void testViewControllersOnWebSphere() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-view-controllers.xml", 16);
|
||||
loadBeanDefinitions("mvc-config-view-controllers.xml", 18);
|
||||
|
||||
SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
|
||||
SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);
|
||||
@@ -469,7 +472,7 @@ public class MvcNamespaceTests {
|
||||
|
||||
@Test
|
||||
public void testViewControllersDefaultConfig() {
|
||||
loadBeanDefinitions("mvc-config-view-controllers-minimal.xml", 4);
|
||||
loadBeanDefinitions("mvc-config-view-controllers-minimal.xml", 6);
|
||||
|
||||
BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
|
||||
assertNotNull(beanNameMapping);
|
||||
@@ -508,6 +511,28 @@ public class MvcNamespaceTests {
|
||||
assertEquals(1, deferredResultInterceptors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathMatchingHandlerMappings() throws Exception {
|
||||
loadBeanDefinitions("mvc-config-path-matching-mappings.xml", 20);
|
||||
|
||||
RequestMappingHandlerMapping requestMapping = appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertNotNull(requestMapping);
|
||||
assertEquals(TestPathHelper.class, requestMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, requestMapping.getPathMatcher().getClass());
|
||||
|
||||
SimpleUrlHandlerMapping viewController = appContext.getBean(VIEWCONTROLLER_BEAN_NAME, SimpleUrlHandlerMapping.class);
|
||||
assertNotNull(viewController);
|
||||
assertEquals(TestPathHelper.class, viewController.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, viewController.getPathMatcher().getClass());
|
||||
|
||||
for(SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) {
|
||||
assertNotNull(handlerMapping);
|
||||
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
|
||||
@@ -618,4 +643,7 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestPathHelper extends UrlPathHelper { }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -35,6 +35,7 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.DefaultMessageCodesResolver;
|
||||
import org.springframework.validation.Errors;
|
||||
@@ -63,6 +64,7 @@ import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* A test fixture with a sub-class of {@link WebMvcConfigurationSupport} that
|
||||
@@ -93,6 +95,8 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
RequestMappingHandlerMapping rmHandlerMapping = webConfig.requestMappingHandlerMapping();
|
||||
rmHandlerMapping.setApplicationContext(webAppContext);
|
||||
rmHandlerMapping.afterPropertiesSet();
|
||||
assertEquals(TestPathHelper.class, rmHandlerMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, rmHandlerMapping.getPathMatcher().getClass());
|
||||
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
|
||||
assertNotNull(chain.getInterceptors());
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
@@ -104,6 +108,8 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
handlerMapping.setApplicationContext(webAppContext);
|
||||
assertNotNull(handlerMapping);
|
||||
assertEquals(1, handlerMapping.getOrder());
|
||||
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
|
||||
HandlerExecutionChain handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
|
||||
assertNotNull(handler.getHandler());
|
||||
|
||||
@@ -111,6 +117,8 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
handlerMapping.setApplicationContext(webAppContext);
|
||||
assertNotNull(handlerMapping);
|
||||
assertEquals(Integer.MAX_VALUE-1, handlerMapping.getOrder());
|
||||
assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass());
|
||||
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
|
||||
handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
|
||||
assertNotNull(handler.getHandler());
|
||||
|
||||
@@ -277,6 +285,12 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
exceptionResolvers.add(new SimpleMappingExceptionResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configurePathMatch(PathMatchConfigurer configurer) {
|
||||
configurer.setPathMatcher(new TestPathMatcher());
|
||||
configurer.setUrlPathHelper(new TestPathHelper());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new LocaleChangeInterceptor());
|
||||
@@ -310,4 +324,7 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
|
||||
}
|
||||
|
||||
private class TestPathHelper extends UrlPathHelper {}
|
||||
|
||||
private class TestPathMatcher extends AntPathMatcher {}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -58,6 +60,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -197,6 +200,16 @@ public class WebMvcConfigurationSupportTests {
|
||||
assertEquals(JsonViewResponseBodyAdvice.class, interceptors.get(0).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPathMatchConfiguration() throws Exception {
|
||||
UrlPathHelper urlPathHelper = this.wac.getBean(UrlPathHelper.class);
|
||||
PathMatcher pathMatcher = this.wac.getBean(PathMatcher.class);
|
||||
|
||||
assertNotNull(urlPathHelper);
|
||||
assertNotNull(pathMatcher);
|
||||
assertEquals(AntPathMatcher.class, pathMatcher.getClass());
|
||||
}
|
||||
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
|
||||
Reference in New Issue
Block a user