Allow HttpMethod as a controller method argument

Issue: SPR-11425
This commit is contained in:
Rossen Stoyanchev
2014-02-13 12:16:24 -05:00
parent b1abe26b33
commit 0cb27f4bc5
5 changed files with 40 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,6 +73,7 @@ import java.util.concurrent.Callable;
* <li>{@link java.io.OutputStream} / {@link java.io.Writer} for generating
* the response's content. This will be the raw OutputStream/Writer as
* exposed by the Servlet/Portlet API.
* <li>{@link org.springframework.http.HttpMethod} for the HTTP request method</li>
* <li>{@link PathVariable @PathVariable} annotated parameters (Servlet-only)
* for access to URI template values (i.e. /hotels/{hotel}). Variable values will be
* converted to the declared method argument type. By default, the URI template

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -235,6 +237,14 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
return sb.toString();
}
/**
* Return the HTTP method of the request.
* @since 4.0.2
*/
public HttpMethod getHttpMethod() {
return HttpMethod.valueOf(getRequest().getMethod().trim().toUpperCase());
}
@Override
public String toString() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,8 +29,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -50,6 +52,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
* <li>{@link java.time.ZoneId} (as of Spring 4.0 and Java 8)</li>
* <li>{@link InputStream}
* <li>{@link Reader}
* <li>{@link org.springframework.http.HttpMethod} (as of Spring 4.0)</li>
* </ul>
*
* @author Arjen Poutsma
@@ -71,7 +74,8 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume
TimeZone.class.equals(paramType) ||
"java.time.ZoneId".equals(paramType.getName()) ||
InputStream.class.isAssignableFrom(paramType) ||
Reader.class.isAssignableFrom(paramType);
Reader.class.isAssignableFrom(paramType) ||
HttpMethod.class.equals(paramType);
}
@Override
@@ -116,6 +120,9 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume
else if (Reader.class.isAssignableFrom(paramType)) {
return request.getReader();
}
else if (HttpMethod.class.equals(paramType)) {
return ((ServletWebRequest) webRequest).getHttpMethod();
}
else {
// should never happen..
Method method = parameter.getMethod();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockHttpSession;
@@ -63,9 +64,9 @@ public class ServletRequestMethodArgumentResolverTests {
public void setUp() throws Exception {
method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class,
HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class,
WebRequest.class, TimeZone.class, ZoneId.class);
WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class);
mavContainer = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest();
servletRequest = new MockHttpServletRequest("GET", "");
webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
}
@@ -213,6 +214,17 @@ public class ServletRequestMethodArgumentResolverTests {
assertSame("Invalid result", webRequest, result);
}
@Test
public void httpMethod() throws Exception {
MethodParameter httpMethodParameter = new MethodParameter(method, 10);
assertTrue("HttpMethod not supported", resolver.supportsParameter(httpMethodParameter));
Object result = resolver.resolveArgument(httpMethodParameter, null, webRequest, null);
assertSame("Invalid result", HttpMethod.valueOf(webRequest.getRequest().getMethod()), result);
}
@SuppressWarnings("unused")
public void supportedParams(ServletRequest p0,
MultipartRequest p1,
@@ -223,7 +235,8 @@ public class ServletRequestMethodArgumentResolverTests {
Reader p6,
WebRequest p7,
TimeZone p8,
ZoneId p9) {
ZoneId p9,
HttpMethod p10) {
}
}

View File

@@ -28946,6 +28946,7 @@ multiple requests are allowed to access a session concurrently.
value is the raw InputStream/Reader as exposed by the Servlet API.
* `java.io.OutputStream` / `java.io.Writer` for generating the response's content. This
value is the raw OutputStream/Writer as exposed by the Servlet API.
* `org.springframework.http.HttpMethod` for the HTTP request method.
* `java.security.Principal` containing the currently authenticated user.
* `@PathVariable` annotated parameters for access to URI template variables. See
<<mvc-ann-requestmapping-uri-templates>>.