springboot 请求处理的常用参数注解 springboot第7期

请求处理-常用参数注解使用

注解:

  • @PathVariable(restful风格获取参数)

  • @RequestHeader(获取请求头)

  • @RequestParam(获取请求参数 ? 的形式)

  • @CookieValue(获取cookie的值)

  • @RequestBody(获取请求体 非get请求)

  • @RequestAttribute(获取request域的值)

  • @MatrixVariable(矩阵变量)

这是我们发生的东西
在这里插入图片描述

PathVariable获取路径变量注解

看一下 PathVariable注解

在这里插入图片描述

如果一个参数的值是map那么 他会将路径里面的kv值都放到map里面
在这里插入图片描述

在这里插入图片描述

@RestController
public class ParameterTestController {

    @GetMapping("/car/{id}/owner/{name}")
    public Map<String,Object> getCar(@PathVariable("id")Integer id ,
                                     @PathVariable("name") 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;
    }


}

这个map我们尝试获取所有的值
在这里插入图片描述
都获取到了
在这里插入图片描述

获取请求头@RequestHeader

在这里插入图片描述
在这里插入图片描述
获取到了

在这里插入图片描述

@RequestParam 获取请求参数(指问号后的参数,url?a=1&b=2)

map和上面的一样都是封装全部
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

@CookieValue 获取Cookie值

@CookieValue里面带的注释
大概意思是我们也可以声明一个Cookie类型 这样我们可以吧所有的Cookie都拿过来
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
有的浏览器没有cookie 这里不用纠结

@RequestBody 获取请求体的数据 (POST请求)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
获取到了
在这里插入图片描述

请求处理-@RequestAttribute 获取request域里面的属性

 @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){

        request.setAttribute("msg","成功了...");
        request.setAttribute("code",200);
        return "forward:/success";  //转发到  /success请求
    }

    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,
                       @RequestAttribute(value = "code",required = false)Integer code,
                       HttpServletRequest request){
        Object msg1 = request.getAttribute("msg");

        Map<String,Object> map = new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");

        map.put("reqMethod_msg",msg1);
        map.put("annotation_msg",msg);
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);

        return map;
    }

这个false的值是可以没有msg这一项
在这里插入图片描述

MatrixVariable与UrlPathHelper

  1. 语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd 这种带 ; 的就是矩阵变量

  2. SpringBoot默认是禁用了矩阵变量的功能

    • 手动开启:原理。对于路径的处理。UrlPathHelper的removeSemicolonContent设置为false,让其支持矩阵变量的。
  3. 矩阵变量必须有url路径变量才能被解析

矩阵变量什么 为什么有这个东西

页面开发,cookie禁用了,session里面的内容怎么使用?

我们在session里面保存了东西
session.set(a,b)—>
jsessionid —>
我们保存的东西在cookie里面
cookie ----> 每次发请求携带。

服务器接收到了cookie 会根据 jsessionid 来找到session对象 然后就可以通过get方法来找到你存了什么
但是如果cookie不让用了 我们session存的东西就会找不到了
为了解决这个问题出现了 url重写 我们吧cookie的值写到url里面
为了与普通变量区别 我们用 ; 做间隔
url重写:/abc;jsesssionid=xxxx 把cookie的值使用矩阵变量的方式进行传递.
举一个例子 /boss/1;age=20/2;age=20
1;age=20
分号前面的是访问路径 分号后面的是矩阵变量

测试

在这里插入图片描述

      @GetMapping("/cars2/{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;
    }

开启矩阵变量

package com.example.springboot_study_02.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.HashMap;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
public class WebConfig {
    //implements WebMvcConfigurer
    @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);
//    }
}

记得开启拦截器

spring:
  resources:
    static-locations: [classpath:/gb/,classpath:/gb1/]
  mvc:
    hiddenmethod:
      filter:
        enabled: true

在这里插入图片描述

题外话

今天是2022年3月25号
我的第一天笔记是3月20号写的也就是说整整五天时间我才看了三十集的网课 可以说我的学习效率相对来说是比较失败的了
主要是学校的课 我是真的不想上 都大三了还天天一堆课 靠学校的那点东西我怕不是毕业就要被饿死
这几天也在看小应学长写的23种设计模式 感觉收获不少
跟小应学长写的东西远比我好 但是我的第一篇文章刚开始两天在排名上比小应学长高我是不能接受的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值