Explicit HEAD sorted higher than implicit GET match
Issue: SPR-14182
This commit is contained in:
parent
ff38224da6
commit
7cdcc102a9
|
@ -39,8 +39,8 @@ import org.springframework.web.cors.CorsUtils;
|
|||
*/
|
||||
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
|
||||
|
||||
private static final RequestMethodsRequestCondition HEAD_CONDITION =
|
||||
new RequestMethodsRequestCondition(RequestMethod.HEAD);
|
||||
private static final RequestMethodsRequestCondition GET_CONDITION =
|
||||
new RequestMethodsRequestCondition(RequestMethod.GET);
|
||||
|
||||
|
||||
private final Set<RequestMethod> methods;
|
||||
|
@ -140,7 +140,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
|
|||
}
|
||||
}
|
||||
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
|
||||
return HEAD_CONDITION;
|
||||
return GET_CONDITION;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -159,7 +159,18 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
|
|||
*/
|
||||
@Override
|
||||
public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
|
||||
return (other.methods.size() - this.methods.size());
|
||||
if (other.methods.size() != this.methods.size()) {
|
||||
return other.methods.size() - this.methods.size();
|
||||
}
|
||||
else if (this.methods.size() == 1) {
|
||||
if (this.methods.contains(RequestMethod.HEAD) && other.methods.contains(RequestMethod.GET)) {
|
||||
return -1;
|
||||
}
|
||||
else if (this.methods.contains(RequestMethod.GET) && other.methods.contains(RequestMethod.HEAD)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public class RequestMethodsRequestConditionTests {
|
|||
@Test
|
||||
public void getMatchingConditionWithHttpHead() {
|
||||
testMatch(new RequestMethodsRequestCondition(HEAD), HEAD);
|
||||
testMatch(new RequestMethodsRequestCondition(GET), HEAD);
|
||||
testMatch(new RequestMethodsRequestCondition(GET), GET);
|
||||
testNoMatch(new RequestMethodsRequestCondition(POST), HEAD);
|
||||
}
|
||||
|
||||
|
|
|
@ -1776,6 +1776,18 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
|||
assertEquals("body", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpHeadExplicit() throws ServletException, IOException {
|
||||
initServletWithControllers(ResponseEntityController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/stores");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("v1", response.getHeader("h1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpOptions() throws ServletException, IOException {
|
||||
initServletWithControllers(ResponseEntityController.class);
|
||||
|
@ -3100,6 +3112,16 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
|||
public ResponseEntity<String> baz() {
|
||||
return ResponseEntity.ok().header("MyResponseHeader", "MyValue").body("body");
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/stores", method = RequestMethod.HEAD)
|
||||
public ResponseEntity<Void> headResource() {
|
||||
return ResponseEntity.ok().header("h1", "v1").build();
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/stores", method = RequestMethod.GET)
|
||||
public ResponseEntity<String> getResource() {
|
||||
return ResponseEntity.ok().body("body");
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
|
|
Loading…
Reference in New Issue