有@RequesMapping标注的方法的可用annotation參數
@RequestParam request參數
@PathVariable 路行參數
@CookieValue Cookie
@RequestBody 請求content body
@RequestHeader 請求頭資訊
HttpEntity<String> entity.
package home.dong.springmvc;
import home.dong.springmvc.beans.User;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/data/*")
public class RequestDataController {
/**
* 顯式指定參數名來擷取慘數值 @RequestParam String foo 例: http://localhost:8080/app_name/data/param?foo=bar
*
* @param foo
* @return
*/
@RequestMapping(value = "param")
public @ResponseBody
String withParam(@RequestParam String foo) {
return "Obtained 'foo' query parameter value '" + foo + "'";
}
/**
* 隐式傳遞并将多個參數轉換成 java bean. 參數名可直接對應java bean的屬性名,這樣spring 将自動轉換為一個bean對象。
* 例:http://localhost:8080/app_name/data/paramsToBean?userName=tiger&age=5&[email protected]
*
* @param user
* @return
*/
@RequestMapping(value = "paramsToBean")
public @ResponseBody
String withParamsToBean(User user) {
return "Obtained parameters and convert to Bean: " + user.getUserName() + "==" + user.getAge() + "==" + user.getEmail();
}
/**
* 混合使用 隐式&顯式傳遞多個參數.将參數名能夠對應java bean屬性的直接轉換成java bean.其他參數用@RequestParam顯式指定并接收。
* 例:http://localhost:8080/app_name/data/paramsToBean?userName=tiger&age=25&[email protected]&foo=bar
*
* @param user
* @return
*/
@RequestMapping(value = "paramGroup")
public @ResponseBody
String withParamGroup(@RequestParam String foo, User user) {
return "Obtained parameter group " + user.getUserName() + "==" + user.getAge() + "==" + user.getEmail() + "<br/>" + "foo====" + foo;
}
/**
* 路徑參數。
*
* 參數作為請求路徑的一部分,并可擷取其值。
* @RequestMapping(value = "path/{myVar}")中 {myVar}必須和 @PathVariable String myVar保持一緻。
* 如 http://localhost:8080/app_name/data/path/foo
* @param var
* @return
*/
@RequestMapping(value = "path/{var}")
public @ResponseBody
String withPathVariable(@PathVariable String var) {
return "Obtained 'var' path variable value '" + var + "'";
}
/**
* 路徑參數。
* @RequestMapping(value = "path/{var}")中 {myVar}必須和 @PathVariable String myVar保持一緻。
* 參數作為請求路徑的一部分,并可擷取其值。同時也可追加其他的參數。
* 如 http://localhost:8080/app_name/data/pathAndParams/foo?userName=tiger&age=5&[email protected]&myfoo=bar
* @param var
* @return
*/
@RequestMapping(value = "pathAndParams/{var}")
public @ResponseBody
String withPathVariableAndParams(@PathVariable String var, User user, @RequestParam String myfoo) {
return "Obtained 'var' path variable value '" + var + "'<br />" + user.getUserName() + "==" + user.getAge() + "==" + user.getEmail() + "<br/>"
+ "myfoo====" + myfoo;
}
/**
* 将cookie中的值綁定到方法參數中。
* @param openid_provider
* @return
*/
@RequestMapping(value = "cookie")
public @ResponseBody
String withCookie(@CookieValue(value="JSESSIONID") String openid_provider) {
return "Obtained 'openid_provider' cookie '" + openid_provider + "'";
}
/**
* The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.
* 将方法中的參數與request context body中的值綁定,可轉換json格式資料,可參照博文 http://starscream.iteye.com/blog/1067606
* @param body
* @return
*/
@RequestMapping(value = "body", method = RequestMethod.POST)
public @ResponseBody
String withBody(@RequestBody String body) {
return "Posted request body By POST'" + body + "'";
}
/**
* 可用來讀取http請求的header和 body内容,注意因為httpentity,@RequestBody,Reader這三種類型的參數都是通過inputstram來讀取httpbody的資料的,
* 而inputstream流不能反複讀取,是以這三種類型的參數不能放在一起使用。而且在沒有設定content-type或沒有設定相應的messageconverter的情況下,都會報錯
* 可轉換json格式資料,可參照博文 http://starscream.iteye.com/blog/1067606
* @param entity
* @return
*/
@RequestMapping(value = "entity", method = RequestMethod.POST)
public @ResponseBody
String withEntity(HttpEntity<String> entity) {
return "Posted request body '" + entity.getBody() + "'; headers = " + entity.getHeaders();
}
/**
* 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
*
* 将handler method中的參數與request header中的值綁定,
* 例如 @RequestHeader(required=false,value="User-Agent") String ua 表示将header中的"User-Agent"與ua這個入參綁定。
* @param host
* @return
*/
@RequestMapping(value = "/headerHost")
public @ResponseBody
String withHeaderHost(@RequestHeader(value = "Host") String host) {
String str = "Obtained header Host=='" + host + "'<br />";
return str;
}
@RequestMapping(value = "/headerAccept")
public @ResponseBody
String withHeaderAccept(@RequestHeader(value = "Accept") String accept) {
String str = "Obtained header Accept=='" + accept + "'<br />";
return str;
}
@RequestMapping(value = "/headerCookie")
public @ResponseBody
String withHeaderCookie(@RequestHeader(value = "Cookie") String cookie) {
String str = "Obtained header Cookie=='" + cookie + "'<br />";
return str;
}
/**
*
* @param language
* @return
*/
@RequestMapping(value = "/headerLanguage")
public @ResponseBody
String withHeaderLanguage(@RequestHeader(value = "Accept-Language") String language) {
String str = "Obtained header Language=='" + language + "'<br />";
return str;
}
/**
*
* @param host
* @param accept
* @param language
* @param encoding
* @param charSet
* @param keepAlive
* @param cookie
* @return
*/
@RequestMapping(value = "header")
public @ResponseBody
String withHeader(@RequestHeader(value = "Host") String host, @RequestHeader(value = "Accept") String accept,
@RequestHeader(value = "Accept-Language") String language, @RequestHeader(value = "Accept-Encoding") String encoding,
@RequestHeader(value = "Accept-Charset") String charSet, @RequestHeader(value = "Keep-Alive") long keepAlive,
@RequestHeader(value = "Cookie") String cookie) {
String str = "Obtained header Host=='" + host + "'<br />";
str += "Obtained header Accept=='" + accept + "'<br />";
str += "Obtained header Accept-Language=='" + language + "'<br />";
str += "Obtained header Accept-Encoding=='" + encoding + "'<br />";
str += "Obtained header Accept-Charset=='" + charSet + "'<br />";
str += "Obtained header Keep-Alive=='" + keepAlive + "'<br />";
str += "Obtained header Cookie=='" + cookie + "'<br />";
return str;
}
}
以上最後的兩個方法會有異常:
Aug 30, 2011 10:11:01 AM org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.service() for servlet appServlet threw exceptionjava.lang.IllegalStateException: Missing header 'Accept-Language' of type [java.lang.String]at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.raiseMissingHeaderException(HandlerMethodInvoker.java:784)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestHeader(HandlerMethodInvoker.java:554)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:343)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)at java.lang.Thread.run(Thread.java:662)
貌似是request header 中找不到 Accept-Language。不知道為什麼,可是手冊上說是有的啊!暫時還沒解決。期待高手出手相助。
JSP
<ul>
<li><a id="param" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/param?foo=bar" />">Query parameter</a></li>
<li><a id="groupBean" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/paramsToBean?userName=tiger&age=5&[email protected]" />">Group of query parameters and
Convert to a User Bean object.</a></li>
<li><a id="group" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/paramGroup?userName=tiger&age=5&[email protected]&foo=bar" />">Group of query parameters</a></li>
<li><a id="var" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/path/foo" />">Path variable</a></li>
<li><a id="varParams" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/pathAndParams/foo?userName=tiger&age=5&[email protected]&myfoo=bar" />">Path variable and
parameters</a></li>
<li><a id="cookie" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/cookie" />">Cookie</a></li>
<li>
<form class="textForm" action="<c:url value="/data/body" />" method="post">
<input type="text" value="lydongss" name="userName"><br /> <input type="text" value="22" name="age"><br /> <input type="text"
value="[email protected]" name="email"> <br /> <input id="requestBody" type="submit" value="Request Body" />
</form></li>
<li>
<form class="textForm" action="<c:url value="/data/entity" />" method="post">
<input type="text" value="22" name="myValue"> <input id="requestBodyAndHeaders" type="submit" value="Request Body and Headers" />
</form></li>
<li><a id="headerHost" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/headerHost" />">headerHost</a></li>
<li><a id="headerAccept" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/headerAccept" />">headerAccept</a></li>
<li><a id="headerCookie" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/headerCookie" />">headerCookie</a></li>
<li><a id="headerLanguage" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/headerLanguage" />">headerLanguage</a></li>
<li><a id="header" class="textLink" href="<c:url value=" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" /data/header" />">Header</a></li>
</ul>