请求时常用的注解

注解1@PathVariable获取路径变量

使用的形式:/car/{id}/owner/{username}
例如:

    @GetMapping("/car/{id}/owner/{username}")
   public Map<String,Object> get(@PathVariable("id") Integer id,
                                 @PathVariable("username") String name,
                                 @PathVariable Map<String,Object> map){
       Map<String,Object> map1= new HashMap<>();
       map1.put("id",id);
       map1.put("username",name);
       map1.put("map",map);
       return map1;
   }

在这里插入图片描述

注解2,@RequestHeader获取请求头

    @GetMapping("/car")
    public Map<String,Object> get(@RequestHeader("User-Agent") String header,
                                  @RequestHeader Map<String,Object> map
    ){
        Map<String,Object> map1= new HashMap<>();
        map1.put("User-Agent",header);
        map1.put("map",map);
        return map1;
    }

在这里插入图片描述

注解3,@RequestParam获取请求参数

    @GetMapping("/car")
    public Map<String,Object> get(@RequestParam("id") Integer id,
                                  @RequestParam("name") String name,
                                  @RequestParam Map<String,Object> map
    ){
        Map<String,Object> map1= new HashMap<>();
        map1.put("id",id);
        map1.put("name",name);
        map1.put("map",map);
        return map1;
    }

在这里插入图片描述

注解4,@CookieValue获取cookie的值

    @GetMapping("/car")
    public Map<String,Object> get(@CookieValue("Idea-8fe00652") String Idea,
                                  @CookieValue("Idea-8fe00652") Cookie cookie
    ){
        Map<String,Object> map1= new HashMap<>();
        map1.put("Idea-8fe00652",Idea);
        map1.put("cookie",cookie);
        return map1;
    }

在这里插入图片描述

注解5,@RequestBody获取请求体的值

<form action="/save" method="post">
    姓名:<input name="姓名" >
    年龄:<input aria="年龄" >
    <input value="button" type="submit">
</form>
    @PostMapping("/save")
    public Map postMethod(@RequestBody String content){
        Map<String,Object> map = new HashMap<>();
        map.put("content",content);
        return map;
    }

在这里插入图片描述

注解6,@RequestAttribute获取request的域属性

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class requestController {
    @GetMapping("/goto")
    public String Goto(HttpServletRequest request){
        request.setAttribute("msg","hah");
        request.setAttribute("mn","xsa");
        return "forward:/sucess";
    }
    @ResponseBody
    @GetMapping("/sucess")
    public Map<String,Object> sucess(HttpServletRequest request,
                                     @RequestAttribute("msg") String msg,
                                     @RequestAttribute("mn") String mn){

        Map<String, Object> map=new HashMap<>();
        Object msg1 = request.getAttribute("msg");
        map.put("mag",msg1);
        map.put("mn",mn);
        return map;
    }
}

在这里插入图片描述

注解7,@MatrixVariable矩阵变量

矩阵变量默认时关闭的,因此首先是要开启矩阵变量
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
开启后进行测试
    //1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd
    //2、SpringBoot默认是禁用了矩阵变量的功能
    //      手动开启:原理。对于路径的处理。UrlPathHelper进行解析。
    //              removeSemicolonContent(移除分号内容)支持矩阵变量的
    //3、矩阵变量必须有url路径变量才能被解析
    @GetMapping("/cars/{path}")
    public Map carsSell(@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;
    }

    // /boss/1;age=20/2;age=10

    @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;
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值