Make RequestMappingHandlerMapping xml config easier

Prior to this commit, it was necessary to override
the HandlerMapping definition to change properties
like useSuffixPatternMatch, useSuffixPatternMatch...
Also, one couldn't set custom pathmatcher/pathhelper
on RequestMappingHandlerMapping via XML configuration.

This commits adds a new "mvc:annotation-driven"
subelement called "mvc:path-matching" for the tag
that allows to configure such properties:
* suffix-pattern
* trailing-slash
* registered-suffixes-only
* path-matcher
* path-helper

Note: this is a new take on this issue, since
96b418cc has been reverted by e2b99c3.

Issue: SPR-10163
This commit is contained in:
Brian Clozel
2014-01-20 20:44:55 +01:00
parent 8edb7a18cc
commit eac4881809
4 changed files with 140 additions and 5 deletions

View File

@@ -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.
@@ -15,12 +15,9 @@
*/
package org.springframework.web.servlet.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
@@ -30,6 +27,7 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.AntPathMatcher;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.WebArgumentResolver;
@@ -42,11 +40,16 @@ import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
import org.springframework.web.util.UrlPathHelper;
import static org.junit.Assert.*;
/**
* Test fixture for the configuration in mvc-config-annotation-driven.xml.
* @author Rossen Stoyanchev
* @author Brian Clozel
*/
public class AnnotationDrivenBeanDefinitionParserTests {
@@ -70,6 +73,21 @@ public class AnnotationDrivenBeanDefinitionParserTests {
assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
}
@Test
public void testPathMatchingConfiguration() {
loadBeanDefinitions("mvc-config-path-matching.xml");
RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
assertNotNull(hm);
assertTrue(hm.useSuffixPatternMatch());
assertFalse(hm.useTrailingSlashMatch());
assertTrue(hm.useRegisteredSuffixPatternMatch());
assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class));
assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class));
List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
assertThat(fileExtensions, Matchers.contains("xml"));
assertThat(fileExtensions, Matchers.hasSize(1));
}
@Test
public void testMessageConverters() {
loadBeanDefinitions("mvc-config-message-converters.xml");
@@ -198,3 +216,7 @@ class TestMessageCodesResolver implements MessageCodesResolver {
}
}
class TestPathMatcher extends AntPathMatcher { }
class TestPathHelper extends UrlPathHelper { }