SPRING MVC获取request参数方法:
@RequestParam /data/param?foo=bar 等价于 request.getParameter()
@PathVariable 获取URL路径 // http://127.0.0.1:8010/data/path/foo
@MatrixVariable 获取URL路径中协调的键值对参数
@RequestHeader 获取RequestHeader 中的信息
@RequestBody 获取request中 流信息
HttpEntity
另外:编程时大都是使用表单提交request中的参数,很少设计二进制流的request提交;演示案例中使用AJAX方法来进行二进制流的提交,见JS代码;注意data属性,它是字符串而不是JSON对象,这里在HTTP传输时使用的是字符流
$("form.textForm").submit(function(event) { var form = $(this); var button = form.children(":first"); $.ajax({ type: "POST", url: form.attr("action"), data: "foo", contentType: "text/plain", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }}); return false; });
见代码:
@Controller
@RequestMapping("/data")
public class RequestDataController {
// /data/param?foo=bar
@RequestMapping(value = "param", method = RequestMethod.GET)
public @ResponseBody
String withParam(@RequestParam String foo) {
return "Obtained 'foo' query parameter value '" + foo + "'";
}
// http://127.0.0.1:8010/data/group?param1=foo¶m2=bar¶m3=baz
@RequestMapping(value = "group", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
String withParamGroup(JavaBean bean) {
return "Obtained parameter group " + bean;
}
@RequestMapping(value = "produces", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
JavaBean byProducesJson(JavaBean bean) {
return bean;
}
// http://127.0.0.1:8010/data/path/foo
@RequestMapping(value = "path/{var}", method = RequestMethod.GET)
public @ResponseBody
String withPathVariable(@PathVariable String var) {
return "Obtained 'var' path variable value '" + var + "'";
}
// http://127.0.0.1:8010/data/matrixvars;foo=bar/simple
// OUTPUT :Obtained matrix variable 'foo=bar' from path segment 'matrixvars'
@RequestMapping(value = "{path}/simple", method = RequestMethod.GET)
public @ResponseBody
String withMatrixVariable(@PathVariable String path,
@MatrixVariable String foo) {
return "Obtained matrix variable 'foo=" + foo + "' from path segment '"
+ path + "'";
}
//http://127.0.0.1:8010/data/matrixvars;foo=bar1/multiple;foo=bar2
//output: Obtained matrix variable foo=bar1 from path segment 'matrixvars' and
// variable 'foo=bar2 from path segment 'multiple'
@RequestMapping(value = "{path1}/{path2}", method = RequestMethod.GET)
public @ResponseBody
String withMatrixVariablesMultiple(@PathVariable String path1,
@MatrixVariable(value = "foo", pathVar = "path1") String foo1,
@PathVariable String path2,
@MatrixVariable(value = "foo", pathVar = "path2") String foo2) {
return "Obtained matrix variable foo=" + foo1 + " from path segment '"
+ path1 + "' and variable 'foo=" + foo2
+ " from path segment '" + path2 + "'";
}
// http://127.0.0.1:8010/data/header
@RequestMapping(value = "header", method = RequestMethod.GET)
public @ResponseBody
String withHeader(@RequestHeader String Accept) {
return "Obtained 'Accept' header '" + Accept + "'";
}
@RequestMapping(value = "cookie", method = RequestMethod.GET)
public @ResponseBody
String withCookie(@CookieValue String openid_provider) {
return "Obtained 'openid_provider' cookie '" + openid_provider + "'";
}
@RequestMapping(value = "body", method = RequestMethod.POST)
public @ResponseBody
String withBody(@RequestBody String body) {
return "Posted request body '" + body + "'";
}
@RequestMapping(value = "entity", method = RequestMethod.POST)
public @ResponseBody
String withEntity(HttpEntity entity) {
return "Posted request body '" + entity.getBody() + "'; headers = "
+ entity.getHeaders();
}
}
另外还有传统的获取方式直接在controller方法中使用HttpServletRequest request ,HttpServletResponse response,HttpSession session ;springMVC就可以直接调用这些变量中保存的内容;
见代码:
@Controller
public class StandardArgumentsController {
// request related
@RequestMapping(value="/data/standard/request", method=RequestMethod.GET)
public @ResponseBody String standardRequestArgs(HttpServletRequest request, Principal user, Locale locale) {
StringBuilder buffer = new StringBuilder();
buffer.append("request = ").append(request).append(", ");
buffer.append("userPrincipal = ").append(user).append(", ");
buffer.append("requestLocale = ").append(locale);
return buffer.toString();
}
@RequestMapping(value="/data/standard/request/reader", method=RequestMethod.POST)
public @ResponseBody String requestReader(Reader requestBodyReader) throws IOException {
return "Read char request body = " + FileCopyUtils.copyToString(requestBodyReader);
}
@RequestMapping(value="/data/standard/request/is", method=RequestMethod.POST)
public @ResponseBody String requestReader(InputStream requestBodyIs) throws IOException {
return "Read binary request body = " + new String(FileCopyUtils.copyToByteArray(requestBodyIs));
}
// response related
@RequestMapping("/data/standard/response")
public @ResponseBody String response(HttpServletResponse response) {
return "response = " + response;
}
@RequestMapping("/data/standard/response/writer")
public void availableStandardResponseArguments(Writer responseWriter) throws IOException {
responseWriter.write("Wrote char response using Writer");
}
@RequestMapping("/data/standard/response/os")
public void availableStandardResponseArguments(OutputStream os) throws IOException {
os.write("Wrote binary response using OutputStream".getBytes());
}
// HttpSession
@RequestMapping("/data/standard/session")
public @ResponseBody String session(HttpSession session) {
StringBuilder buffer = new StringBuilder();
buffer.append("session=").append(session);
return buffer.toString();
}
}
最后上传界面截图: