Skip to content

Commit 0cb27f4

Browse files
committed
Allow HttpMethod as a controller method argument
Issue: SPR-11425
1 parent b1abe26 commit 0cb27f4

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -73,6 +73,7 @@
7373
* <li>{@link java.io.OutputStream} / {@link java.io.Writer} for generating
7474
* the response's content. This will be the raw OutputStream/Writer as
7575
* exposed by the Servlet/Portlet API.
76+
* <li>{@link org.springframework.http.HttpMethod} for the HTTP request method</li>
7677
* <li>{@link PathVariable @PathVariable} annotated parameters (Servlet-only)
7778
* for access to URI template values (i.e. /hotels/{hotel}). Variable values will be
7879
* converted to the declared method argument type. By default, the URI template

spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -24,6 +24,8 @@
2424
import javax.servlet.http.HttpServletResponse;
2525
import javax.servlet.http.HttpSession;
2626

27+
import org.springframework.http.HttpMethod;
28+
import org.springframework.http.HttpStatus;
2729
import org.springframework.util.CollectionUtils;
2830
import org.springframework.util.ObjectUtils;
2931
import org.springframework.util.StringUtils;
@@ -235,6 +237,14 @@ public String getDescription(boolean includeClientInfo) {
235237
return sb.toString();
236238
}
237239

240+
/**
241+
* Return the HTTP method of the request.
242+
* @since 4.0.2
243+
*/
244+
public HttpMethod getHttpMethod() {
245+
return HttpMethod.valueOf(getRequest().getMethod().trim().toUpperCase());
246+
}
247+
238248

239249
@Override
240250
public String toString() {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -29,8 +29,10 @@
2929
import javax.servlet.http.HttpSession;
3030

3131
import org.springframework.core.MethodParameter;
32+
import org.springframework.http.HttpMethod;
3233
import org.springframework.web.bind.support.WebDataBinderFactory;
3334
import org.springframework.web.context.request.NativeWebRequest;
35+
import org.springframework.web.context.request.ServletWebRequest;
3436
import org.springframework.web.context.request.WebRequest;
3537
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
3638
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -50,6 +52,7 @@
5052
* <li>{@link java.time.ZoneId} (as of Spring 4.0 and Java 8)</li>
5153
* <li>{@link InputStream}
5254
* <li>{@link Reader}
55+
* <li>{@link org.springframework.http.HttpMethod} (as of Spring 4.0)</li>
5356
* </ul>
5457
*
5558
* @author Arjen Poutsma
@@ -71,7 +74,8 @@ public boolean supportsParameter(MethodParameter parameter) {
7174
TimeZone.class.equals(paramType) ||
7275
"java.time.ZoneId".equals(paramType.getName()) ||
7376
InputStream.class.isAssignableFrom(paramType) ||
74-
Reader.class.isAssignableFrom(paramType);
77+
Reader.class.isAssignableFrom(paramType) ||
78+
HttpMethod.class.equals(paramType);
7579
}
7680

7781
@Override
@@ -116,6 +120,9 @@ else if (InputStream.class.isAssignableFrom(paramType)) {
116120
else if (Reader.class.isAssignableFrom(paramType)) {
117121
return request.getReader();
118122
}
123+
else if (HttpMethod.class.equals(paramType)) {
124+
return ((ServletWebRequest) webRequest).getHttpMethod();
125+
}
119126
else {
120127
// should never happen..
121128
Method method = parameter.getMethod();

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -30,6 +30,7 @@
3030
import org.junit.Test;
3131

3232
import org.springframework.core.MethodParameter;
33+
import org.springframework.http.HttpMethod;
3334
import org.springframework.mock.web.test.MockHttpServletRequest;
3435
import org.springframework.mock.web.test.MockHttpServletResponse;
3536
import org.springframework.mock.web.test.MockHttpSession;
@@ -63,9 +64,9 @@ public class ServletRequestMethodArgumentResolverTests {
6364
public void setUp() throws Exception {
6465
method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class,
6566
HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class,
66-
WebRequest.class, TimeZone.class, ZoneId.class);
67+
WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class);
6768
mavContainer = new ModelAndViewContainer();
68-
servletRequest = new MockHttpServletRequest();
69+
servletRequest = new MockHttpServletRequest("GET", "");
6970
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
7071
}
7172

@@ -213,6 +214,17 @@ public void webRequest() throws Exception {
213214
assertSame("Invalid result", webRequest, result);
214215
}
215216

217+
@Test
218+
public void httpMethod() throws Exception {
219+
MethodParameter httpMethodParameter = new MethodParameter(method, 10);
220+
221+
assertTrue("HttpMethod not supported", resolver.supportsParameter(httpMethodParameter));
222+
223+
Object result = resolver.resolveArgument(httpMethodParameter, null, webRequest, null);
224+
assertSame("Invalid result", HttpMethod.valueOf(webRequest.getRequest().getMethod()), result);
225+
}
226+
227+
216228
@SuppressWarnings("unused")
217229
public void supportedParams(ServletRequest p0,
218230
MultipartRequest p1,
@@ -223,7 +235,8 @@ public void supportedParams(ServletRequest p0,
223235
Reader p6,
224236
WebRequest p7,
225237
TimeZone p8,
226-
ZoneId p9) {
238+
ZoneId p9,
239+
HttpMethod p10) {
227240
}
228241

229242
}

src/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28946,6 +28946,7 @@ multiple requests are allowed to access a session concurrently.
2894628946
value is the raw InputStream/Reader as exposed by the Servlet API.
2894728947
* `java.io.OutputStream` / `java.io.Writer` for generating the response's content. This
2894828948
value is the raw OutputStream/Writer as exposed by the Servlet API.
28949+
* `org.springframework.http.HttpMethod` for the HTTP request method.
2894928950
* `java.security.Principal` containing the currently authenticated user.
2895028951
* `@PathVariable` annotated parameters for access to URI template variables. See
2895128952
<<mvc-ann-requestmapping-uri-templates>>.

0 commit comments

Comments
 (0)