Skip to content

Commit 7cdcc10

Browse files
committed
Explicit HEAD sorted higher than implicit GET match
Issue: SPR-14182
1 parent ff38224 commit 7cdcc10

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
*/
4040
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
4141

42-
private static final RequestMethodsRequestCondition HEAD_CONDITION =
43-
new RequestMethodsRequestCondition(RequestMethod.HEAD);
42+
private static final RequestMethodsRequestCondition GET_CONDITION =
43+
new RequestMethodsRequestCondition(RequestMethod.GET);
4444

4545

4646
private final Set<RequestMethod> methods;
@@ -140,7 +140,7 @@ private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue
140140
}
141141
}
142142
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
143-
return HEAD_CONDITION;
143+
return GET_CONDITION;
144144
}
145145
}
146146
return null;
@@ -159,7 +159,18 @@ private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue
159159
*/
160160
@Override
161161
public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
162-
return (other.methods.size() - this.methods.size());
162+
if (other.methods.size() != this.methods.size()) {
163+
return other.methods.size() - this.methods.size();
164+
}
165+
else if (this.methods.size() == 1) {
166+
if (this.methods.contains(RequestMethod.HEAD) && other.methods.contains(RequestMethod.GET)) {
167+
return -1;
168+
}
169+
else if (this.methods.contains(RequestMethod.GET) && other.methods.contains(RequestMethod.HEAD)) {
170+
return 1;
171+
}
172+
}
173+
return 0;
163174
}
164175

165176
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void getMatchingCondition() {
5353
@Test
5454
public void getMatchingConditionWithHttpHead() {
5555
testMatch(new RequestMethodsRequestCondition(HEAD), HEAD);
56-
testMatch(new RequestMethodsRequestCondition(GET), HEAD);
56+
testMatch(new RequestMethodsRequestCondition(GET), GET);
5757
testNoMatch(new RequestMethodsRequestCondition(POST), HEAD);
5858
}
5959

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,18 @@ public void httpHead() throws ServletException, IOException {
17761776
assertEquals("body", response.getContentAsString());
17771777
}
17781778

1779+
@Test
1780+
public void httpHeadExplicit() throws ServletException, IOException {
1781+
initServletWithControllers(ResponseEntityController.class);
1782+
1783+
MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/stores");
1784+
MockHttpServletResponse response = new MockHttpServletResponse();
1785+
getServlet().service(request, response);
1786+
1787+
assertEquals(200, response.getStatus());
1788+
assertEquals("v1", response.getHeader("h1"));
1789+
}
1790+
17791791
@Test
17801792
public void httpOptions() throws ServletException, IOException {
17811793
initServletWithControllers(ResponseEntityController.class);
@@ -3100,6 +3112,16 @@ public ResponseEntity<Void> bar() {
31003112
public ResponseEntity<String> baz() {
31013113
return ResponseEntity.ok().header("MyResponseHeader", "MyValue").body("body");
31023114
}
3115+
3116+
@RequestMapping(path = "/stores", method = RequestMethod.HEAD)
3117+
public ResponseEntity<Void> headResource() {
3118+
return ResponseEntity.ok().header("h1", "v1").build();
3119+
}
3120+
3121+
@RequestMapping(path = "/stores", method = RequestMethod.GET)
3122+
public ResponseEntity<String> getResource() {
3123+
return ResponseEntity.ok().body("body");
3124+
}
31033125
}
31043126

31053127
@Controller

0 commit comments

Comments
 (0)