springBoot普通参数与基本注解

普通参数与基本注解

@PathVariable、@RequestParam、@RequestHeader、@CookieValue、@RequestAttribute、@MatrixVariable、@RequestBody
以上注解作用在控制器方法中的形参中
@PathVariable:获取rest风格请求路径中的请求参数
@RequestParam:获取原始请求方式中的请求参数
@RequestHeader:获取请求中的请求头信息
@CookieValue:获取请求中的Cookie值
rest方式和原始方式结合

<a href="/car/lisi/12?gender=男&inters=足球&inters=篮球">传参数</a>

控制器方法

@GetMapping("/car/{name}/{age}")
    public Map<String,Object> demo1(@PathVariable("name") String name,
                                    @PathVariable("age")Integer age,
                                    @PathVariable Map<String,String> pv,
                                    @RequestHeader("user-agent") String userAgent,
                                    @RequestHeader Map<String,String> header,
                                    @RequestParam("gender") String gender,
                                    @RequestParam("inters")List<String> inters,
                                    @RequestParam Map<String,String> param,
                                    @CookieValue("Pycharm-eea02815") String _ga,
                                    @CookieValue("Pycharm-eea02815") Cookie cookie
                                    ){

        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        map.put("pv",pv);
        map.put("userAgent",userAgent);
        map.put("header",header);
        map.put("gender",gender);
        map.put("inters",inters);
        map.put("param",param);
        map.put("_ga",_ga);
        map.put("cookie",cookie);
        return map;
    }

@RequestAttribute:获取共享数据域对象中的值

@RequestMapping("/go")
    public String go(HttpServletRequest request){

        request.setAttribute("msg","成功了");
        request.setAttribute("code",200);

        return "forward:/to";
    }

    @ResponseBody
    @RequestMapping("/to")
    public Map to(
                  @RequestAttribute("msg") String msg,
                  @RequestAttribute("code") Integer code,
                  HttpServletRequest request){
        Object msg1 = request.getAttribute("msg");
        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg",msg);
        map.put("msg1",msg1);
        return map;
    }

@RequestBody:获取请求体,请求体中包含请求参数

<form action="/save" method="post">
    姓名:<input type="text" name="username">
    密码:<input type="password" name="pass">
    <input type="submit" value="提交">
</form>

controller方法

@RequestMapping("/save")
    public Map demo2(
            @RequestBody String body   //获取请求体,提交表单方式必须是post
    ){
        Map<String,Object> map=new HashMap<>();
        map.put("body",body);
        return map;
    }

测试:
在这里插入图片描述

结果:
在这里插入图片描述

@MatrixVariable:矩阵变量
虽然从Spring 3.2就已经支持==@MatrixVariable==特性,但直至现在其依然为默认禁用的状态。我们需要手工配置开启才能使用
配置类:实现WebMvcConfigurer接口

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

控制器方法:

@RestController
public class ParameterTestController {

    //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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Keep up

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

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

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

打赏作者

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

抵扣说明:

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

余额充值