SPR-5772 - Annotation handler method matching doesn't get method/param choice right
This commit is contained in:
parent
ddcd9f4905
commit
ea2ece4516
|
|
@ -820,7 +820,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
|||
* Comparator capable of sorting {@link RequestMappingInfo}s (RHIs) so that sorting a list with this comparator will
|
||||
* result in: <ul> <li>RHIs with {@linkplain RequestMappingInfo#matchedPaths better matched paths} take prescedence
|
||||
* over those with a weaker match (as expressed by the {@linkplain PathMatcher#getPatternComparator(String) path
|
||||
* pattern comparator}.) Typically, this means that patterns without wild chards and uri templates will be ordered
|
||||
* pattern comparator}.) Typically, this means that patterns without wild cards and uri templates will be ordered
|
||||
* before those without.</li> <li>RHIs with one single {@linkplain RequestMappingInfo#methods request method} will be
|
||||
* ordered before those without a method, or with more than one method.</li> <li>RHIs with more {@linkplain
|
||||
* RequestMappingInfo#params request parameters} will be ordered before those with less parameters</li> </ol>
|
||||
|
|
@ -838,6 +838,16 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
|||
if (pathComparison != 0) {
|
||||
return pathComparison;
|
||||
}
|
||||
int info1ParamCount = info1.params.length;
|
||||
int info2ParamCount = info2.params.length;
|
||||
if (info1ParamCount != info2ParamCount) {
|
||||
return info2ParamCount - info1ParamCount;
|
||||
}
|
||||
int info1HeaderCount = info1.headers.length;
|
||||
int info2HeaderCount = info2.headers.length;
|
||||
if (info1HeaderCount != info2HeaderCount) {
|
||||
return info2HeaderCount - info1HeaderCount;
|
||||
}
|
||||
int info1MethodCount = info1.methods.length;
|
||||
int info2MethodCount = info2.methods.length;
|
||||
if (info1MethodCount == 0 && info2MethodCount > 0) {
|
||||
|
|
@ -852,16 +862,6 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
|||
else if (info2MethodCount == 1 & info1MethodCount > 1) {
|
||||
return 1;
|
||||
}
|
||||
int info1ParamCount = info1.params.length;
|
||||
int info2ParamCount = info2.params.length;
|
||||
if (info1ParamCount != info2ParamCount) {
|
||||
return info2ParamCount - info1ParamCount;
|
||||
}
|
||||
int info1HeaderCount = info1.headers.length;
|
||||
int info2HeaderCount = info2.headers.length;
|
||||
if (info1HeaderCount != info2HeaderCount) {
|
||||
return info2HeaderCount - info1HeaderCount;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -974,6 +974,22 @@ public class ServletAnnotationControllerTests {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ambiguousParams() throws ServletException, IOException {
|
||||
initServlet(AmbiguousParamsController.class);
|
||||
|
||||
MockHttpServletRequest request;// = new MockHttpServletRequest("GET", "/test");
|
||||
MockHttpServletResponse response;// = new MockHttpServletResponse();
|
||||
// servlet.service(request, response);
|
||||
// assertEquals("noParams", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/test");
|
||||
request.addParameter("myParam", "42");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("myParam-42", response.getContentAsString());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Controllers
|
||||
|
|
@ -1647,6 +1663,21 @@ public class ServletAnnotationControllerTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/test*")
|
||||
private static class AmbiguousParamsController {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public void noParams(Writer writer) throws IOException {
|
||||
writer.write("noParams");
|
||||
}
|
||||
|
||||
@RequestMapping(params = "myParam")
|
||||
public void param(@RequestParam("myParam") int myParam, Writer writer) throws IOException {
|
||||
writer.write("myParam-" + myParam);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue