Controller中获取输入参数注解使用总结
2016-08-22 11:56
447 查看
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中获取输入参数注解使用总结
- Controller中获取输入参数注解使用总结
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
- 使用 aop拦截 springMVC的controller并获取请求参数及返回结果
- shell 参数列表的获取&shell使用的一些总结
- SpringMVC Controller 的使用及请求参数的获取
- PowerShell函数中使用$PSBoundParameters获取输入参数列表实例
- [ java]注解的使用2————Type 获取包下面有@Controller的注解 demo
- 使用参数拦截器通过注解直接从JSON对象中获取数据
- AOP 切面的使用,以及如何在通知上获取切入方法的注解和参数
- [译]DbContext API中使用SqlQuery和ExecuteSqlCommand获取存储过程的输入输出参数
- 【亲测】SpringBoot04_controller类中注解对url及url参数的使用
- spring aop使用注解从切面中获取参数
- 注解实现AOP(获取输入及返回参数)
- SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据
- shell 参数列表的获取&shell使用的一些总结
- (亲测有效。)Spring MVC中,Controller中使用service只需使用注解,但是普通类获取 service或 dao 都是null空的。
- spring boot中controller的使用及url参数的获取方法
- !!!.NET多线程使用总结——传入参数与获取返回值
- MasterPage中使用SiteMapPath获取参数 “当前上下文不可用”解决办法