Skip to content

Commit 736b3c4

Browse files
committed
BEST_MATCHING_HANDLER_ATTRIBUTE for spring-webmvc
Issue: SPR-17518
1 parent 617b94a commit 736b3c4

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-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
@@ -371,6 +371,7 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
371371
request.getRequestURL() + "': {" + m1 + ", " + m2 + "}");
372372
}
373373
}
374+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.handlerMethod);
374375
handleMatch(bestMatch.mapping, lookupPath, request);
375376
return bestMatch.handlerMethod;
376377
}

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
@@ -418,6 +418,7 @@ public PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWit
418418
@Override
419419
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
420420
exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
421+
request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler);
421422
request.setAttribute(INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
422423
return true;
423424
}

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: 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)