SPR-9062 Fix bug with ambiguous path and HTTP method request mappings

A direct path match with incorrect HTTP request method was causing another
request mapping with a pattern and a correct HTTP method to be ignored.
The bug affects the new @MVC support classes
(i.e. RequestMappingHandlerMapping).
This commit is contained in:
Rossen Stoyanchev
2012-01-30 19:52:20 -05:00
parent 21966990c8
commit e9208981a6
3 changed files with 48 additions and 11 deletions

View File

@@ -18,12 +18,14 @@ package org.springframework.web.servlet.handler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -237,18 +239,16 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
* @see #handleNoMatch(Set, String, HttpServletRequest)
*/
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
List<T> mappings = urlMap.get(lookupPath);
if (mappings == null) {
mappings = new ArrayList<T>(handlerMethods.keySet());
}
List<Match> matches = new ArrayList<Match>();
for (T mapping : mappings) {
T match = getMatchingMapping(mapping, request);
if (match != null) {
matches.add(new Match(match, handlerMethods.get(mapping)));
}
List<T> directPathMatches = this.urlMap.get(lookupPath);
if (directPathMatches != null) {
addMatchingMappings(directPathMatches, matches, request);
}
if (matches.isEmpty()) {
// No choice but to go through all mappings
addMatchingMappings(this.handlerMethods.keySet(), matches, request);
}
if (!matches.isEmpty()) {
@@ -279,6 +279,15 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
}
}
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {
for (T mapping : mappings) {
T match = getMatchingMapping(mapping, request);
if (match != null) {
matches.add(new Match(match, handlerMethods.get(mapping)));
}
}
}
/**
* Check if a mapping matches the current request and return a (potentially
* new) mapping with conditions relevant to the current request.