Skip to content

Commit a55ca56

Browse files
committed
BEST_MATCHING_HANDLER_ATTRIBUTE for spring-webmvc
Issue: SPR-17518
1 parent d6a5c34 commit a55ca56

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-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-2016 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.
@@ -55,6 +55,13 @@
5555
*/
5656
public interface HandlerMapping {
5757

58+
/**
59+
* Name of the {@link HttpServletRequest} attribute that contains the mapped
60+
* handler for the best matching pattern.
61+
* @since 5.1.3
62+
*/
63+
String BEST_MATCHING_HANDLER_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingHandler";
64+
5865
/**
5966
* Name of the {@link HttpServletRequest} attribute that contains the path
6067
* 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
@@ -414,6 +414,7 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
414414
"Ambiguous handler methods mapped for '" + uri + "': {" + m1 + ", " + m2 + "}");
415415
}
416416
}
417+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.handlerMethod);
417418
handleMatch(bestMatch.mapping, lookupPath, request);
418419
return bestMatch.handlerMethod;
419420
}

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
@@ -411,6 +411,7 @@ public PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWit
411411
@Override
412412
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
413413
exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
414+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler);
414415
request.setAttribute(INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
415416
return true;
416417
}

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
@@ -36,6 +36,7 @@
3636
import org.springframework.web.context.support.StaticWebApplicationContext;
3737
import org.springframework.web.cors.CorsConfiguration;
3838
import org.springframework.web.method.HandlerMethod;
39+
import org.springframework.web.servlet.HandlerMapping;
3940
import org.springframework.web.util.UrlPathHelper;
4041

4142
import static org.junit.Assert.assertEquals;
@@ -81,17 +82,21 @@ public void directMatch() throws Exception {
8182
String key = "foo";
8283
this.mapping.registerMapping(key, this.handler, this.method1);
8384

84-
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
85+
MockHttpServletRequest request = new MockHttpServletRequest("GET", key);
86+
HandlerMethod result = this.mapping.getHandlerInternal(request);
8587
assertEquals(method1, result.getMethod());
88+
assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
8689
}
8790

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

93-
HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
96+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
97+
HandlerMethod result = this.mapping.getHandlerInternal(request);
9498
assertEquals(method1, result.getMethod());
99+
assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
95100
}
96101

97102
@Test(expected = IllegalStateException.class)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public class SimpleUrlHandlerMappingTests {
4242

4343
@Test
4444
@SuppressWarnings("resource")
45-
public void handlerBeanNotFound() throws Exception {
45+
public void handlerBeanNotFound() {
4646
MockServletContext sc = new MockServletContext("");
4747
XmlWebApplicationContext root = new XmlWebApplicationContext();
4848
root.setServletContext(sc);
49-
root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"});
49+
root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml");
5050
root.refresh();
5151
XmlWebApplicationContext wac = new XmlWebApplicationContext();
5252
wac.setParent(root);
5353
wac.setServletContext(sc);
5454
wac.setNamespace("map2err");
55-
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2err.xml"});
55+
wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml");
5656
try {
5757
wac.refresh();
5858
fail("Should have thrown NoSuchBeanDefinitionException");
@@ -95,7 +95,7 @@ private void checkMappings(String beanName) throws Exception {
9595
MockServletContext sc = new MockServletContext("");
9696
XmlWebApplicationContext wac = new XmlWebApplicationContext();
9797
wac.setServletContext(sc);
98-
wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2.xml"});
98+
wac.setConfigLocations("/org/springframework/web/servlet/handler/map2.xml");
9999
wac.refresh();
100100
Object bean = wac.getBean("mainController");
101101
Object otherBean = wac.getBean("otherController");
@@ -106,11 +106,13 @@ private void checkMappings(String beanName) throws Exception {
106106
HandlerExecutionChain hec = getHandler(hm, req);
107107
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
108108
assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
109+
assertEquals(bean, req.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
109110

110111
req = new MockHttpServletRequest("GET", "/welcome.x");
111112
hec = getHandler(hm, req);
112113
assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
113114
assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
115+
assertEquals(otherBean, req.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
114116

115117
req = new MockHttpServletRequest("GET", "/welcome/");
116118
hec = getHandler(hm, req);

0 commit comments

Comments
 (0)