Skip to content

Commit 96b418c

Browse files
committed
Make RequestMappingHandlerMapping xml config easier
Prior to this commit, it was necessary to override the HandlerMapping definition to change properties like useSuffixPatternMatch, useSuffixPatternMatch... This commits adds new attributes on the mvc:annotation-driven XML tag that allows to configure such flags: * use-suffix-pattern-match * use-trailing-slash-match * use-registered-suffix-pattern-match Issue: SPR-10163
1 parent c1f3da0 commit 96b418c

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ else if (element.hasAttribute("enableMatrixVariables")) {
171171
Boolean enableMatrixVariables = Boolean.valueOf(element.getAttribute("enableMatrixVariables"));
172172
handlerMappingDef.getPropertyValues().add("removeSemicolonContent", !enableMatrixVariables);
173173
}
174+
if(element.hasAttribute("use-suffix-pattern-match")) {
175+
handlerMappingDef.getPropertyValues().add("useSuffixPatternMatch",
176+
Boolean.valueOf(element.getAttribute("use-suffix-pattern-match")));
177+
}
178+
if(element.hasAttribute("use-trailing-slash-match")) {
179+
handlerMappingDef.getPropertyValues().add("useTrailingSlashMatch",
180+
Boolean.valueOf(element.getAttribute("use-trailing-slash-match")));
181+
}
182+
if(element.hasAttribute("use-registered-suffix-pattern-match")) {
183+
handlerMappingDef.getPropertyValues().add("useRegisteredSuffixPatternMatch",
184+
Boolean.valueOf(element.getAttribute("use-registered-suffix-pattern-match")));
185+
}
186+
174187

175188
RuntimeBeanReference conversionService = getConversionService(element, source, parserContext);
176189
RuntimeBeanReference validator = getValidator(element, source, parserContext);

spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.0.xsd

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,35 @@
255255
]]></xsd:documentation>
256256
</xsd:annotation>
257257
</xsd:attribute>
258+
<xsd:attribute name="use-suffix-pattern-match" type="xsd:boolean">
259+
<xsd:annotation>
260+
<xsd:documentation><![CDATA[
261+
Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled
262+
a method mapped to "/users" also matches to "/users.*". The default value is true.
263+
]]></xsd:documentation>
264+
</xsd:annotation>
265+
</xsd:attribute>
266+
<xsd:attribute name="use-trailing-slash-match" type="xsd:boolean">
267+
<xsd:annotation>
268+
<xsd:documentation><![CDATA[
269+
Whether to match to URLs irrespective of the presence of a trailing slash.
270+
If enabled a method mapped to "/users" also matches to "/users/".
271+
The default value is true.
272+
]]></xsd:documentation>
273+
</xsd:annotation>
274+
</xsd:attribute>
275+
<xsd:attribute name="use-registered-suffix-pattern-match" type="xsd:boolean">
276+
<xsd:annotation>
277+
<xsd:documentation><![CDATA[
278+
Whether to use suffix pattern match for registered file extensions only when matching patterns to requests.
279+
If enabled, a controller method mapped to "/users" also matches to "/users.json" assuming ".json" is a file extension registered with
280+
the provided ContentNegotiationManager. This can be useful for allowing only specific URL extensions to be used as well as in cases
281+
where a "." in the URL path can lead to ambiguous interpretation of path variable content, (e.g. given "/users/{user}" and incoming
282+
URLs such as "/users/john.j.joe" and "/users/john.j.joe.json").
283+
If enabled, this attribute also enables use-suffix-pattern-match. The default value is false.
284+
]]></xsd:documentation>
285+
</xsd:annotation>
286+
</xsd:attribute>
258287
</xsd:complexType>
259288
</xsd:element>
260289

spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,6 @@
1515
*/
1616
package org.springframework.web.servlet.config;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
21-
2218
import java.util.List;
2319

2420
import org.junit.Before;
@@ -42,11 +38,16 @@
4238
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
4339
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
4440
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
41+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
4542
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
4643

44+
import static org.hamcrest.Matchers.*;
45+
import static org.junit.Assert.*;
46+
4747
/**
4848
* Test fixture for the configuration in mvc-config-annotation-driven.xml.
4949
* @author Rossen Stoyanchev
50+
* @author Brian Clozel
5051
*/
5152
public class AnnotationDrivenBeanDefinitionParserTests {
5253

@@ -70,6 +71,28 @@ public void testMessageCodesResolver() {
7071
assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
7172
}
7273

74+
@Test
75+
public void testCustomContentNegotiationManager() {
76+
loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");
77+
RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
78+
assertNotNull(hm);
79+
assertTrue(hm.useSuffixPatternMatch());
80+
assertTrue(hm.useRegisteredSuffixPatternMatch());
81+
List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
82+
assertThat(fileExtensions, contains("xml"));
83+
assertThat(fileExtensions, hasSize(1));
84+
}
85+
86+
@Test
87+
public void testRequestMappingCustomAttributes() {
88+
loadBeanDefinitions("mvc-config-custom-attributes.xml");
89+
RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
90+
assertNotNull(hm);
91+
assertFalse(hm.getUrlPathHelper().shouldRemoveSemicolonContent());
92+
assertFalse(hm.useTrailingSlashMatch());
93+
assertFalse(hm.useSuffixPatternMatch());
94+
}
95+
7396
@Test
7497
public void testMessageConverters() {
7598
loadBeanDefinitions("mvc-config-message-converters.xml");

spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-content-negotiation-manager.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

8-
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
8+
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"
9+
use-registered-suffix-pattern-match="true"/>
910

1011
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
1112
<property name="mediaTypes">
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:mvc="http://www.springframework.org/schema/mvc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
7+
8+
<mvc:annotation-driven
9+
use-trailing-slash-match="false"
10+
use-suffix-pattern-match="false"
11+
enable-matrix-variables="true"
12+
/>
13+
</beans>

0 commit comments

Comments
 (0)