@RequestMapping("/")
public String handle(@RequestAttribute Client client) {
// ...
}
@RequestAttribute, @CookieValue and @RequestHeader
Using @RequestAttribute to access request attributes
Similar to @SessionAttribute
the @RequestAttribute
annotation can be used to
access pre-existing request attributes created by a filter or interceptor:
Working with "application/x-www-form-urlencoded" data
The previous sections covered use of @ModelAttribute
to support form submission
requests from browser clients. The same annotation is recommended for use with requests
from non-browser clients as well. However there is one notable difference when it comes
to working with HTTP PUT requests. Browsers can submit form data via HTTP GET or HTTP
POST. Non-browser clients can also submit forms via HTTP PUT. This presents a challenge
because the Servlet specification requires the ServletRequest.getParameter*()
family
of methods to support form field access only for HTTP POST, not for HTTP PUT.
To support HTTP PUT and PATCH requests, the spring-web
module provides the filter
HttpPutFormContentFilter
, which can be configured in web.xml
:
<filter>
<filter-name>httpPutFormFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormFilter</filter-name>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
The above filter intercepts HTTP PUT and PATCH requests with content type
application/x-www-form-urlencoded
, reads the form data from the body of the request,
and wraps the ServletRequest
in order to make the form data available through the
ServletRequest.getParameter*()
family of methods.
Note
|
As |
Mapping cookie values with the @CookieValue annotation
The @CookieValue
annotation allows a method parameter to be bound to the value of an
HTTP cookie.
Let us consider that the following cookie has been received with an http request:
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
The following code sample demonstrates how to get the value of the JSESSIONID
cookie:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
//...
}
Type conversion is applied automatically if the target method parameter type is not
String
. See [mvc-ann-typeconversion].
This annotation is supported for annotated handler methods in Servlet and Portlet environments.
Mapping request header attributes with the @RequestHeader annotation
The @RequestHeader
annotation allows a method parameter to be bound to a request header.
Here is a sample request header:
Host localhost:8080 Accept text/html,application/xhtml+xml,application/xml;q=0.9 Accept-Language fr,en-gb;q=0.7,en;q=0.3 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300
The following code sample demonstrates how to get the value of the Accept-Encoding
and
Keep-Alive
headers:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
}
Type conversion is applied automatically if the method parameter is not String
. See
[mvc-ann-typeconversion].
When an @RequestHeader
annotation is used on a Map<String, String>
,
MultiValueMap<String, String>
, or HttpHeaders
argument, the map is populated
with all header values.
Tip
|
Built-in support is available for converting a comma-separated string into an
array/collection of strings or other types known to the type conversion system. For
example a method parameter annotated with |
This annotation is supported for annotated handler methods in Servlet and Portlet environments.