Controller中获取输入参数注解使用总结
1.处理request的uri部分的参数(即restful访问方式):@PathVariable.
当使用restful访问方式时, 即 someUrl/{paramId}, 这时的参数可通过 @Pathvariable注解来获取。
调用方式(get方法):http://localhost:4005/***/cxhdlb/111111
接收参数代码:
@RequestMapping(value = "/cxhdlb/{param}", method = RequestMethod.GET)
public List<String> findEventList(@PathVariable String param) {
System.out.println(param);
}
2.处理request header部分的参数:@RequestHeader,@CookieValue
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
这是一个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
接收参数代码:

@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
}
上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。
例如有如下Cookie值:
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
接收参数代码:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
//...
}
即把JSESSIONID的值绑定到参数cookie上。
3.处理request body部分的注解:@RequestParam,@RequestBody,@Validated
@RequestParam注解用来接收地址中的参数,参数的格式是http://*****?uid=111111&uname=张三。
接收参数代码:
@Controller
@RequestMapping("/users")
public class UserController{
@RequestMapping(value = "/hqyhxx",method = RequestMethod.GET)
public String getUserInfo(@RequestParam("uid") String uid,@RequestParam("uname") String uname) {
//...
}
}
@Validated注解可以用一个模型来接收地址栏中的参数,参数的格式是http://*****?uid=111111&uname=张三。
接收参数代码:
@Controller
@RequestMapping("/users")
public class UserController{
@RequestMapping(value = "/hqyhxx",method = RequestMethod.GET)
public String getUserInfo(@Validated User user) {
String uid = user.getUid();
String uname = user.getUname();
}
}
@RequestBody注解用来接收request的body中的参数,@RequestBody可以将多个参数放入到一个实体类或者Map中。
接收参数代码:
@RequestMapping(value = "/cjhd", method = RequestMethod.POST)
public Result createEvent(@RequestBody ParameterModel parameterModel, HttpServletRequest request,
HttpServletResponse response) {
String rowkey = parameterModel.getRowkey();
}
或者
@RequestMapping(value = "/cjhd", method = RequestMethod.POST)
public Result createEvent(@RequestBody Map<String, Object> paramMap, HttpServletRequest request,
HttpServletResponse response) {
String rowkey = (String) paramMap.get("rowkey");
}
参考:
http://www.cnblogs.com/qq78292959/p/3760702.html
Controller中获取输入参数注解使用总结的更多相关文章
- Spring注解之Controller中获取请求参数及验证使用
1.处理request的uri部分的参数:@PathVariable. 2.处理request header部分的参数:@RequestHeader,@CookieValue@RequestHeade ...
- Springboot中使用自定义参数注解获取 token 中用户数据
使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...
- struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式
本文演示了JSP中获取HTTP参数的几种方式,还有action中获取HTTP参数的几种方式. 1. 创建JSP页面(testParam.jsp) <%@ page language=" ...
- Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法
一.Mybatis中用#{}和${}获取输入参数的区别 1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值.例如 <mapper namespace=&q ...
- Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载
Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- 【2017-06-27】Js中获取地址栏参数、Js中字符串截取
一.Js中获取地址栏参数 //从地址栏获取想要的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" ...
- larave 控制器中获取路由参数
Laravel中获取路由参数Route Parameters的五种方法示例 作者:SeekerLiu 这篇文章主要给大家介绍了关于Laravel中获取路由参数Route Parameters的五种方法 ...
随机推荐
- Sql Server专题一:索引(下)
首先这次的内容是全文索引,跟前面讲的其实没有多大关系 两种索引的功能和结构都是不同的,普通索引的结构主要以B+树和哈希索引为主,用于实现对字段中数据的精确查找,比如查找某个字段值等于给定值的记录,A= ...
- 阿里云CentOS 7.1使用yum安装MySql5.6.24
正确的安装方法: 众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本.所以我们需要先安装带有当前可用的m ...
- 如何让用户在用webview访问网页时嵌入我们自己的内容
代码如下: NSString *strUrl=[textField text]; NSString *urlString=[NSString stringWithFormat:st ...
- html5 web worker
A web worker is a JavaScript running in the background, without affecting the performance of the pag ...
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...
- checkbook全选/反选/全不选
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- multiset集合容器的集合运算:并、交、差
set和multiset的内部通常是采用平衡二叉树来实现.当放入元素时,会按照一定的排序方法自动排序,默认是按照less<>排序规则来排序.这种自动排序的特性加速了元素查找的过程,但问题是 ...
- BZOJ 3674 可持久化并查集加强版 可持久化并查集
题目大意:同3673 强制在线 同3673 仅仅只是慢了一些0.0 这道题仅仅写路径压缩比仅仅写启示式合并要快一点点 两个都写就慢的要死0.0 改代码RE的可能是内存不够 #include<cs ...
- The 5th tip of DB Query Analyzer
The 5th tip of DB Query Analyzer Ma Genfeng (Guangdong UnitollServices incorporated, G ...
- 编写javascript的基本技巧
第一.编写可维护的代码 什么叫着编写可维护的代码呢?就是当我的做出来的项目,拿给其它编码团队能很快的看懂 你编写的代码,你的整个项目的逻辑等等.一个项目的修改维护是要比开发一个项目的成本 是要高的.例 ...