栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot2.0---------------10、SpringBoot普通参数与基本注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot2.0---------------10、SpringBoot普通参数与基本注解

1. 注解


@PathVariable:映射 URL 绑定的占位符,通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的传入参数中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的传入参数中;



@RequestHeader:@RequestHeader 是获取请求头中的数据如图所示,通过指定参数 value 的值来获取请求头中指定的参数值。其他参数用法和 @RequestParam 完全一样;



@ModelAttribute:


@RequestParam:获取请求参数


请求映射对应的代码:

    /car/{id}/owner/{username}


@MatrixVariable:矩阵变量,矩阵变量应该绑定在路径变量中
/car/{path}?xxx=xxx&aaa=ccc : queryString 查询字符串 使用@RequestParam查询path所携带的参数
/car/{path;low=34;brand=byd,audi,yd}: 矩阵变量,使用@MatrixVariable查询path所携带参数,path中携带两个属性,路径与属性之间以及属性与属性之间用分号隔开,一个属性中对应多个属性值之间用逗号隔开,写法如下:

@MatrixVariable(矩阵变量)
@MatrixVariable(矩阵变量) @MatrixVariable(矩阵变量)/boss/{bossId}/{empId}
  //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 brand,
                        @PathVariable("path") String path){
        Map map = new HashMap<>();

        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    // https://blog.csdn.net/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 map = new HashMap<>();

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

    }

面试题:页面开发:cookie禁用了,session里面的内容怎么使用:
给session保存了某个数据,如session.set(a,b);每个session有一个jsessionid被保存在cookie里面;cookie每次发请求都会携带,若cookie没有被禁用,通过cookie中的jsessionid就可以找到session对象,而后通过session对象的get方法就能找打session里面的内容。
若cookie被禁,则就无法通过jsessionid获取session对象。因此通过矩阵变量的方式获取session中的内容:
例如:通过URL重写:/abc;jsessionid=xxx,把cookie是的值使用矩阵变量的方式进行传递


@CookieValue:获取Cookie值


@RequestBody:获取请求体的值,只有Post请求才有请求体


/save表单提交代码

测试@RequestBody获取数据
用户名:
邮箱:


@RequestAttribute注解:获取request域中所保存的属性值

@Controller
public class RequestController {

    @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("msg") String msg,
                       @RequestAttribute("code")Integer code,
                       HttpServletRequest request){
        Object msg1 = request.getAttribute("msg");

        Map map = new HashMap<>();
        map.put("reqMethod",msg1);
        map.put("annotation",msg);

        return map;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号