Skip to content

Commit abacc6d

Browse files
committed
BEST_MATCHING_HANDLER_ATTRIBUTE for spring-webmvc
Issue: SPR-17518
1 parent 8d668ac commit abacc6d

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

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

Lines changed: 8 additions & 1 deletion
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-2018 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.
@@ -53,6 +53,13 @@
5353
*/
5454
public interface HandlerMapping {
5555

56+
/**
57+
* Name of the {@link HttpServletRequest} attribute that contains the mapped
58+
* handler for the best matching pattern.
59+
* @since 4.3.21
60+
*/
61+
String BEST_MATCHING_HANDLER_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingHandler";
62+
5663
/**
5764
* Name of the {@link HttpServletRequest} attribute that contains the path
5865
* within the handler mapping, in case of a pattern match, or the full

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
367367
request.getRequestURL() + "': {" + m1 + ", " + m2 + "}");
368368
}
369369
}
370+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.handlerMethod);
370371
handleMatch(bestMatch.mapping, lookupPath, request);
371372
return bestMatch.handlerMethod;
372373
}

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWit
407407
@Override
408408
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
409409
exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
410+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler);
410411
request.setAttribute(INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
411412
return true;
412413
}

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMethodMappingTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.web.context.support.StaticWebApplicationContext;
3939
import org.springframework.web.cors.CorsConfiguration;
4040
import org.springframework.web.method.HandlerMethod;
41+
import org.springframework.web.servlet.HandlerMapping;
4142
import org.springframework.web.util.UrlPathHelper;
4243

4344

@@ -79,17 +80,21 @@ public void directMatch() throws Exception {
7980
String key = "foo";
8081
this.mapping.registerMapping(key, this.handler, this.method1);
8182

82-
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
83+
MockHttpServletRequest request = new MockHttpServletRequest("GET", key);
84+
HandlerMethod result = this.mapping.getHandlerInternal(request);
8385
assertEquals(method1, result.getMethod());
86+
assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
8487
}
8588

8689
@Test
8790
public void patternMatch() throws Exception {
8891
this.mapping.registerMapping("/fo*", this.handler, this.method1);
8992
this.mapping.registerMapping("/f*", this.handler, this.method2);
9093

91-
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
94+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
95+
HandlerMethod result = this.mapping.getHandlerInternal(request);
9296
assertEquals(method1, result.getMethod());
97+
assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
9398
}
9499

95100
@Test(expected = IllegalStateException.class)

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@
4141
public class SimpleUrlHandlerMappingTests {
4242

4343
@Test
44-
public void handlerBeanNotFound() throws Exception {
44+
@SuppressWarnings("resource")
45+
public void handlerBeanNotFound() {
4546
MockServletContext sc = new MockServletContext("");
4647
XmlWebApplicationContext root = new XmlWebApplicationContext();
4748
root.setServletContext(sc);
48-
root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"});
49+
root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml");
4950
root.refresh();
5051
XmlWebApplicationContext wac = new XmlWebApplicationContext();
5152
wac.setParent(root);
5253
wac.setServletContext(sc);
5354
wac.setNamespace("map2err");
54-
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2err.xml"});
55+
wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml");
5556
try {
5657
wac.refresh();
5758
fail("Should have thrown NoSuchBeanDefinitionException");
@@ -93,7 +94,7 @@ private void checkMappings(String beanName) throws Exception {
9394
MockServletContext sc = new MockServletContext("");
9495
XmlWebApplicationContext wac = new XmlWebApplicationContext();
9596
wac.setServletContext(sc);
96-
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2.xml"});
97+
wac.setConfigLocations("/org/springframework/web/servlet/handler/map2.xml");
9798
wac.refresh();
9899
Object bean = wac.getBean("mainController");
99100
Object otherBean = wac.getBean("otherController");
@@ -104,11 +105,13 @@ private void checkMappings(String beanName) throws Exception {
104105
HandlerExecutionChain hec = getHandler(hm, req);
105106
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
106107
assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
108+
assertEquals(bean, req.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
107109

108110
req = new MockHttpServletRequest("GET", "/welcome.x");
109111
hec = getHandler(hm, req);
110112
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
111113
assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
114+
assertEquals(otherBean, req.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
112115

113116
req = new MockHttpServletRequest("GET", "/welcome/");
114117
hec = getHandler(hm, req);

0 commit comments

Comments
 (0)