From 0cb27f4bc50e4ce78c64176a0aec1291754cabfd Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 13 Feb 2014 12:16:24 -0500 Subject: [PATCH] Allow HttpMethod as a controller method argument Issue: SPR-11425 --- .../web/bind/annotation/RequestMapping.java | 3 ++- .../context/request/ServletWebRequest.java | 12 ++++++++++- .../ServletRequestMethodArgumentResolver.java | 11 ++++++++-- ...letRequestMethodArgumentResolverTests.java | 21 +++++++++++++++---- src/asciidoc/index.adoc | 1 + 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java index 77e8ef5d99..23faf37860 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java @@ -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; *
  • {@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. + *
  • {@link org.springframework.http.HttpMethod} for the HTTP request method
  • *
  • {@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 diff --git a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java index 0e62a0af77..d653a4be82 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java @@ -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() { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java index 7be1a8a4f5..011704fda5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java @@ -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; *
  • {@link java.time.ZoneId} (as of Spring 4.0 and Java 8)
  • *
  • {@link InputStream} *
  • {@link Reader} + *
  • {@link org.springframework.http.HttpMethod} (as of Spring 4.0)
  • * * * @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(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolverTests.java index 706cfacd5b..d96edc5bdb 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolverTests.java @@ -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) { } } \ No newline at end of file diff --git a/src/asciidoc/index.adoc b/src/asciidoc/index.adoc index 6fcb274ead..e47be553da 100644 --- a/src/asciidoc/index.adoc +++ b/src/asciidoc/index.adoc @@ -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 <>.