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的五种方法 ...
随机推荐
- SAR数据下载网站
1] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.144.2153&rep=rep1&type=pdf[2] ht ...
- HeadFirst设计模式读书笔记(2)-观察者模式(Observer Pattern)
观察者模式:定义了对象之间一对多的依赖关系,这样一来,当一个对象的状态发生改变时,它的依赖者将会受到通知并且自动更新. 有一个模式可以帮你的对象知悉现况,不会错过该对象感兴趣的事,对象甚至在运行时可以 ...
- windows下开发PHP扩展(无需Cygwin)
第一步:准备 1.php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio/Common/MSDev98/Bin的绝对路径添加到win ...
- C语言的本质(20)——预处理之二:条件预处理和包含头文件
我们可以通过定义不同的宏来决定编译程序对哪些代码进行处理.条件编译指令将决定那些代码被编译,而哪些是不被编译的.可以根据表达式的值或者某个特定的宏是否被定义来确定编译条件. 条件编译可分为三种情况,按 ...
- (转载博文)MFC 窗口句柄获取
句柄获取方法(获取该窗口的句柄后,即可向该窗口类类发送消息.处理程序):0.获取所在类窗口的句柄: this->m_hwnd 1.主窗口的句柄: 无论在主窗口类内,还是子窗口类内,获取主窗口句柄 ...
- 【DSA MOOC】有序向量二分查找的三个 版本
内容来自 TsinghuaX: 30240184X 数据结构(2015秋) 课程的Vector一章,对有序向量的二分查找有三个版本 三个版本的函数原型是一致的,都是 Rank search(T con ...
- hdu 1754 I Hate It_线段树
题意:略 思路:套hdu1166模版改改就行了,要注意的是,网上有的代码是错的,还贴出来... #include <iostream> #include<cstdio> #in ...
- rand,randn,randi函数区别
1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数 rand(m,n,'double')生成指定精度的 ...
- public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思
就是说T必须是一个类(class)类型,不能是结构(structure)类型. 这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...