spring-boot 基本注解

​​​​​​注解类型:

  1. @PathVariable :路径变量

  2. @RequestHeader:获取请求头

  3. @RequestParam :获取请求参数

  4. @CookieValue:获取cookie的值

  5. @RequestBody : 获取请求体[post]

  6. ​​​​@RequestAttribute 获取request域属性

  7. @RequestAttribute 获取request域属性

  8. @MatrixVariable 矩阵变量

详细说明

  • @PathVariable :路径变量
  • @RequestHeader:获取请求头
  • @RequestParam :获取请求参数
  • @CookieValue:获取cookie的值
<a href="param/1/name/2?age=28&insterting=baseball">PathVariable</a>
@RestController
public class PamaretertController {

    @GetMapping(value = "/param/{id}/name/{name}")
    public Map<String,Object> get(@PathVariable("id") String id,
                                  @PathVariable("name") String name,
                                  @PathVariable Map<String,String> pv,
                                  @RequestHeader("User-Agent")  String userAgent,
                                  @RequestHeader Map<String,String> header,
                                  @RequestParam("age") Integer age,
                                  @RequestParam("insterting")List<String> ins,
                                  @RequestParam Map<String,String>  params,
                                  @CookieValue("_ga") Cookie cookie
                                  ){
        Map<String,Object> map1 = new HashMap<>();
//        map1.put("id",id);
//        map1.put("name",name);
//        map1.put("pv",pv);
//        map1.put("header",header);
//        map1.put("userAgent",userAgent);
//        map1.put("age",age);
//        map1.put("ins",ins);
//        map1.put("params",params);

        map1.put("cookie",cookie);

        return map1;
    }

}

  • @RequestBody : 获取请求体[post]
<form action="/user/save" method="post">
    用户名:<input type="text" name="name"> </br>
    邮箱:<input type="text" name="email">
    <button type="submit">提交</button>
</form>
@PostMapping(value = "/user/save")
public Map<String,Object>  save(@RequestBody String content){
    Map<String,Object> map = new HashMap<>();
    map.put("content",content);
    return  map;
}
  • @RequestAttribute 获取request域属性
@Controller
public class RequestController {

    @GetMapping(value = "/goto")
    public String gotoPage(HttpServletRequest request){
        request.setAttribute("msg","成功了");
        request.setAttribute("code",100);
        return "forward:success";
    }

    @ResponseBody
    @GetMapping(value ="/success")
    public Map success(@RequestAttribute("msg") String msg,
                          @RequestAttribute("code") Integer code,
                          HttpServletRequest request){
        Map<String,Object> map = new HashMap();
        map.put("msg",msg);
        map.put("code",code);
        map.put("request_msg",request.getAttribute("msg"));
        return  map;
    }
}
  • @MatrixVariable 矩阵变量

springboot 默认是禁止矩阵变量功能,手动开启原理:

 

两种方法:  urlPathHelper.setRemoveSemicolonContent(false);

1、实现WebMvcConfigurer

@Configuration
public class Myconfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

}

2、自定义WebMvcConfigurer

@Bean
public WebMvcConfigurer getWebMvcConfigurer(){
    return new WebMvcConfigurer() {
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            UrlPathHelper urlPathHelper = new UrlPathHelper();
            //不移除后面分号的内容,矩阵变量功能可以生效
              urlPathHelper.setRemoveSemicolonContent(false);
              configurer.setUrlPathHelper(urlPathHelper);
        }
    };
}

使用:

<a href="/car/sell;low=34;brand=byd,audi,yd">@MatrixVariable</a>
<a href="/car/sell;low=34;brand=byd;brand=audi;brand=yd">@MatrixVariable</a>
<a href="/boss/1;age=20/2;age=10">@MatrixVariable</a>

例子1

@GetMapping(value = "/car/{path}")
public Map<String,Object>  car(@MatrixVariable("low") Integer low,
                               @MatrixVariable("brand") List<String> brand,
                               @PathVariable("path") String path){
    Map<String,Object> map = new HashMap<>();
    map.put("low",low);
    map.put("brand",brand);
    map.put("path",path);
    return  map;
}

例2

@GetMapping(value = "/boss/{bossId}/{empId}")
public Map<String,Object>  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;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

心系代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值