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的五种方法 ...
随机推荐
- HeadFirst设计模式读书笔记(4)-工厂模式
工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. 所有工厂模式都用来封装对象的创建.工厂方法模式通过让子类决定该创建的对象是什么,来达到将对象 ...
- jquery hover延时
var timer; //绑定hover事件 $(function () { $(".centercy img").hover(showPic, hid ...
- linux上应用随机启动
这是个go项目,其他的可以参考. 首先要有个脚本比如demo #!/bin/bash # # etcd This shell script takes care of starting and sto ...
- webshell 匿名用户(入侵者)
“web”的含义是显然需要服务器开放web服务,“shell”的含义是取得对服务器某种程度上操作权限.webshell常常被称为匿名用户(入侵者)通过网站端口对网站服务器的某种程度上操作的权限.由于w ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- Nim游戏博弈
Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输. 后来,在一份资料上看到,这种游戏称为"拈(Nim) ...
- IOS设备设计完整指南(转载)
http://blog.sina.com.cn/s/blog_6cc9af670102vq12.html
- 后缀数组da3模板
在做poj2406的时候...按论文给的rmq模板会超内存...然后网上找了http://blog.csdn.net/libin56842/article/details/46310425这位大爷的d ...
- ActionScript简单实现Socket Tcp应用协议分析器
转自..smark http://www.cnblogs.com/smark/archive/2012/05/15/2501507.html ActionScript简单实现Socket Tcp应用协 ...
- (转)flash的Socket通讯沙箱和安全策略问题
一.沙箱和安全策略问题 1.此问题发生在连接时,准确地说是连接前,分别两种情况: 1.本地播放 本地播放时,默认情况下Flash Player将不允许swf访问任何网络. 访问http://www.m ...