Spring常用请求参数注解

注解

  • @PathVariable : 路径变量,如果
    @GetMapping("/test/{id}/user/{userName}")
    public void testPathVariable(@PathVariable("id") Integer id,
                                 @PathVariable("userName") String userName,
                                 //通过Map可以将所有路径参数存入(注:必须用String,String装载)
                                 @PathVariable Map<String,String> allPathVariable){
        return;
    }
  • @RequestHeader : 获取请求头
    @GetMapping("/test")
    public void testHeader(@RequestHeader("User-Agent") String  userAgent,
                           @RequestHeader Map<String,String> allHeader){
        return;
    }
  • @RequestParam :获取请求头参数中的参数提供给指定参数使用
public String login(@RequestParam String userName,
                        @RequestParam("psw") String password,
                        ){
  • @CookieValue :获取Cookie的值
  • @RequestAttribute :获取request域属性
    @GetMapping("/goto")
    public String gotoPage(HttpServletRequest httpServletRequest){
        httpServletRequest.setAttribute("msg","这是我给success的消息");
        httpServletRequest.setAttribute("code",200);
        return "forward:/user/success";
    }

    @ResponseBody
    @GetMapping("/success")
    public void successPage(@RequestAttribute("msg") String msgFromGoto){
        System.out.println("我从GOTO转发到这儿啦,他给我说:"+msgFromGoto);
    }
  • @RequestBody :获取请求体
  • @MatrixVariable:矩阵变量,矩阵变量应该绑定在路径变量中;
    例如当cookie被禁用时,使用url重写:/abc;jsessionid=xxxx;把cookie的值使用矩阵变量的方式进行传递;例如: /cars/1;age=20;/2 分号;前面为访问路径,后边为矩阵变量,多个矩阵变量用分号分割
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false); //将移除;后边的内容,设置为false,默认值为true;
        configurer.setUrlPathHelper(urlPathHelper);
    }
    //springboot默认禁用了矩阵变量功能,需要手动开启
    //对于路径的处理,通过WebMvcConfigurer中的UrlPathHelper进行解析,其中removeSemicolonContent设置是否移除分号后的内容,默认为true
    //http://localhost:9090/user/matrix/unitId=112;name=123
    //必须有请求路径时,才能使用矩阵变量
    @GetMapping("/matrix/{path1}/{path2}")
    public Map matrixVarTest(@MatrixVariable(pathVar = "path1") Integer unitId,@MatrixVariable String name,@MatrixVariable(value = "unitId",pathVar ="path2") Integer unitId2 ){
        Map<String,Object> map = new HashMap<>();
        map.put("unidId",unitId);
        map.put("name",name);
        map.put("unitId2",unitId2);
        return map;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值