SpringBoot常用注解

@PathVariable @RequestHeader @ModelAttribute @RequestParam @MatrixVariable @CookieValue @RequestBody @RequestAttribute

 @PathVariable 用于获取路径中的参数

像映射路径 @GetMapping("/user/{id}/owner/{username}") 可以使用@PathVariable注解获取大括号中的id和usernane的值
@PathVariable("id") String id,  //获取路径中的id
@PathVariable("username") String username,  //获取路径中的username

 @RequestHeader 用于获取请求的头信息

@RequestHeader("User-Agent") String header,  //获取的单个User-Agent头信息
@RequestHeader Map<String,String> headers,  //获取 所有的头信息

获取所有的头信息使用map集合并且里面都是String类型

头信息还有很多,可以F12在RequestHeader获取想要的信息

 @RequestParam 用于获取携带的请求参数

@PathVariable做区分,此注解用于获取例如

localhost:8080/user/1/owner/zz?age=15&inters=play%20basketball&inters=watch%20moving

若要获取所有的请求参数,可以使用map

其中?后面的 age和inters对应的值是使用@RequestParam注解获取,而id和username是使用@PathVariable获取

 @CookieValue 用于获取Cookie

@CookieValue("_ga") String _ga,                //获取_ga的Cookie的值
@CookieValue("_ga") Cookie cookies){           //获取所有_ga的Cookie的值

其中可以直接用Cookie获取cookies对象

 

 

@RequestBody  用于获取请求体,可以获取表单全部的数据

@ResponseBody              //获取表单的数据
@RequestMapping("/loginjudge")
public Map<String, Object> reBody(@RequestBody String content) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("content", content);
    return map;
}

若要获取表单的单个数据可以使用  HttpServletRequest request 获取

String username = request.getParameter("username");
String password = request.getParameter("password");

 

@RequestAttribute 获取request域属性

在 HttpServletRequest 中setAttribute,在另外一个页面通过@RequestAttribute获取Attribute的值,为了取出请求域中的数据必须是转发,俩次获取的msg是同一个值.

 另外还可以使用俩次 HttpServletRequest request,获取的同样也是一个值

 @MatrixVariable 矩阵变量

SpringBoot默认是禁用了矩阵变量的功能,需要手动开启,开启方法:在Webconfig类中注入Bean

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return  new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);

            }
        };
    }

queryString 风格   /user/{id}/owner?xxx=xxx&xxx=xxx  xxx对应的值使用@RequestParam注解获取

rest风格:  /user/{id}/xxx/xxx

矩阵变量风格:/user/{id};name=xxx;inters=xxx,xxx

矩阵变量,使用;来分割不同的请求参数

//user/1;name=zwz;inters=basketball,football
    @ResponseBody
    @GetMapping("/user/{id}")
    public Map carsSell(@MatrixVariable("name") String name,
                        @MatrixVariable("inters") List<String> inters,
                        @PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<>();

        map.put("name",name);
        map.put("inters",inters);
        map.put("id",id);
        return map;
    }


//当路径中获取的矩阵变量有重名的使用pathVar指定在哪一层路径下的值
 @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        Map<String,Object> map = new HashMap<>();

        map.put("bossAge",bossAge);
        map.put("empAge",empAge);
        return map;

    }

 

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天你学习了ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值