SpringBoot-常用参数注解

@PathVariable
  <a href="/Card/1/小黄">@PathVariable(路径变量)</a>
 @GetMapping("/Card/{id}/{username}")
    public Map<String ,Object> getCard(@PathVariable("id") Integer id,
                                       @PathVariable("username") String name,
                                       @PathVariable Map<String, String> pv){
        
        Map<String,Object> map =  new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        return map;

    }
结果:

@PathVariable

@RequestHeader
<a href="/getHeader">@RequestHeader(获取请求头)</a>
   @GetMapping("/getHeader")
    public Map<String,Object> getHeader(@RequestHeader("Pragma") String Pragma,
                                        @RequestHeader Map<String,String> allHeader){

        Map<String,Object> map =  new HashMap<>();
        map.put("Pragma",Pragma);
        map.put("allHeader",allHeader);
        return map;
    }
结果:

@RequestHeader

@RequestParam
<a href="/getParam?age=1&interest='吃饭'&interest='睡觉'">@RequestParam(获取请求参数)</a>
 @GetMapping("/getParam")
    public Map<String,Object> getParam(@RequestParam("age")Integer age,
                                       @RequestParam("interest") List<String> interest){
        Map<String,Object> map =  new HashMap<>();
        map.put("age",age);
        map.put("interest",interest);
        return map;
    }
结果:

RequestParam

@RequestBody
<form method="post" action="/getPerson" >
   名字: <input type="text" name="name">
   年龄:<input type="text" name="age">
   <input type="submit">
</form>
  @PostMapping(value = "/getPerson")
    public Map<String,Object> getPerson(@RequestBody String Person){
        Map<String,Object> map = new HashMap<>();
        map.put("Person",Person);
        return map;
    }
结果:

@RequestBody

@RequestAttribute

比如:request.setAttribute(“haha”);请求转发到另外一个映射中。
我们就可以通过@RequestAttribute(“haha”)获取到haha保存的值
等同于request.getAttribute(“haha”);

可以获取到 请求域中的值~
@MatrixVariable

矩阵变量必须有url路径变量才能被解析~

springboot默认关闭了矩阵变量功能(可以通过以下两种方法开启)
//方法一
   @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper  urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
//方法二
  @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
例一:(只有一个路径变量~)
<a href="/person/id;name=xiao;interest=吃饭,睡觉,打豆豆">@MatrixVariable(矩阵变量)</a>
    @RequestMapping("/person/{path}")
    public Map<String,Object> getCars(@MatrixVariable("name") String name,
                                      @MatrixVariable("interest") List<String> interest,
                                      @PathVariable("path") String path){

        Map<String,Object> map =  new HashMap<>();
        map.put("name",name);
        map.put("interest",interest);
        map.put("path",path);
        return map;
    }
结果:在这里插入图片描述
例二:(当有两个路径变量的时候!)
<a href="/person/one;age=20/two;age=10">@MatrixVariable(矩阵变量)/person/{path1}/{path2}</a>
 @RequestMapping("/person/{path1}/{path2}")
    public Map<String,Object> getCars(@MatrixVariable(value = "age",pathVar = "path1") Integer path1 ,
                                      @MatrixVariable(value = "age",pathVar = "path2") Integer path2,
                                      @PathVariable("path1") String pathname1,
                                      @PathVariable("path2") String pathname2){

        Map<String,Object> map =  new HashMap<>();
        map.put("path1", path1);
        map.put("path2", path2);
        map.put("pathname1",pathname1);
        map.put("pathname2",pathname2);
        return map;
    }
结果:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值