zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    随机验证码生成
    python之map和filter
    Json学习笔记
    动态规划求区间最值问题RMQ(Range Minimum/Maximum Query)
    积水问题
    5亿个数找中位数
    Linux下进程间通信:命名管道mkfifo
    Trie树总结
    树的公共祖先问题LCA
    类文件结构
  • 原文地址:https://www.cnblogs.com/Jason-Xiang/p/5795077.html
Copyright © 2011-2022 走看看