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中获取输入参数注解使用总结的更多相关文章

  1. Spring注解之Controller中获取请求参数及验证使用

    1.处理request的uri部分的参数:@PathVariable. 2.处理request header部分的参数:@RequestHeader,@CookieValue@RequestHeade ...

  2. Springboot中使用自定义参数注解获取 token 中用户数据

    使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...

  3. struts2:JSP页面及Action中获取HTTP参数(parameter)的几种方式

    本文演示了JSP中获取HTTP参数的几种方式,还有action中获取HTTP参数的几种方式. 1. 创建JSP页面(testParam.jsp) <%@ page language=" ...

  4. Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法

    一.Mybatis中用#{}和${}获取输入参数的区别 1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值.例如 <mapper namespace=&q ...

  5. Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

    Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...

  6. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  7. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  8. 【2017-06-27】Js中获取地址栏参数、Js中字符串截取

    一.Js中获取地址栏参数 //从地址栏获取想要的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" ...

  9. larave 控制器中获取路由参数

    Laravel中获取路由参数Route Parameters的五种方法示例 作者:SeekerLiu 这篇文章主要给大家介绍了关于Laravel中获取路由参数Route Parameters的五种方法 ...

随机推荐

  1. Oracle EBS-SQL (OM-3):销售连接停靠站时冲减库存出错处理.sql

    DELETE FROM INV.MTL_RESERVATIONS MRWHERE EXISTS (SELECT 1 FROM WSH.WSH_DELIVERY_ASSIGNMENTS WDA      ...

  2. JIT和程序的首次执行

    由于C#源代码经过编译器编译生成的是IL代码,而IL是与CPU无关的机器语言.因此当程序运行于特定的CPU时,首先必须将IL转换成本地CPU指令,这正是JIT(Just-In-Time)编译器的任务. ...

  3. OpenstackUbuntu

    1,create user

  4. 改变DM6467的内存划分

    上次改过bbxm的http://blog.csdn.net/godofdsp/article/details/9377515,这次搞6467又遇到同样的问题了.按照bbxm的方法修改了内存划分,运行时 ...

  5. Makefile与Shell的问题

    http://blog.csdn.net/absurd/article/details/636418 Makefile与Shell的问题 大概只要知道Makefile的人,都知道Makefile可以调 ...

  6. iOS 自我检測

    1.id 和 NSObject的差别? 2.UITableViewCell的复用原理? 3.UIView生命周期和UILayer的差别? 4.多线程NSOperation和Queue.GDC.Thre ...

  7. Mobile Web开发 处理设备的横竖屏

    为了应对移动设备屏幕的碎片化,我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间.由于屏幕存在着方向性,用户在切换了屏幕的方向后,有些设计上或实现上的 ...

  8. linux下的时间及时区设置

    一.时间设置及同步 1修改系统时间 #date -s 06/18/14 #date -s 14:20:50 2命令查看.设置硬件时间 #hwclock  --show 或者clock  --show ...

  9. inline函数

    C语言中的inline函数并不是单纯的用函数块内容来替换,也可能存在局部变量啥的.另外,使用inline定义的函数只是建议编译器作为内联函数处理,但并不一定真会这样处理.inline一般直接在头文件中 ...

  10. asp.net mvc ajax提交例子

    @{ Layout = null; } <script src="../../Scripts/jquery-1.10.2.min.js" type="text/ja ...