带你了解接收参数@PathVariable、@ModelAttribute 等相关注解

😀前言
本篇博文是关于SpringBoot 接收客户端提交数据/参数会使用到的相关注解应用说明,希望能够帮助到您😊

🏠个人主页:晨犀主页
🧑个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满意是我的动力😉😉
💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
如果文章有什么需要改进的地方还请大佬不吝赐教 先在次感谢啦😊

SpringBoot接收参数相关注解应用

基本介绍

SpringBoot 接收客户端提交数据/参数会使用到相关注解

详解

@PathVariable 、@RequestHeader 、@ModelAttribute 、@RequestParam 、@MatrixVariable、@CookieValue、@RequestBody

接收参数相关注解应用实例

需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收

应用实例演示

● 需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收

image-20230813080413037

创建03_web\src\main\resources\public\index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>index</title>
    </head>
    <body>
        <h1>hello, 天天开心呀</h1>
        基本注解:
        <hr/>
        <a href="/monster/200/jack">@PathVariable-路径变量 monster/200/jack</a><br/><br/>
    </body>
</html>
@PathVariable
代码实例

演示@PathVariable 使用,创建com/nlc/web/controller/ParameterController.java ,完成测试

@RestController
public class ParameterController {
    /**
     * /monster/{id}/{name} 解读
     * 1. /monster/{id}/{name} 构成完整请求路径
     * 2. {id} {name} 就是占位变量
     * 3. @PathVariable("name"): 这里name 和{name} 命名保持一致
     * 4. String name 这里可以自定义
     * 5. @PathVariable Map<String, String> map 把所有传递的值传入map
     * 6. 可以看下@PathVariable源码
     */
    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) {
        System.out.println("id-" + id);
        System.out.println("name-" + name);
        System.out.println("map-" + map);
        return "success";
    }
}
测试结果

image-20230813081157833

image-20230813081222657

@RequestHeader
代码实例

演示@RequestHeader 使用,修改ParameterController.java , 完成测试
1.修改index.html

<a href="requestHeader">@RequestHeader-获取Http 请求头</a><br/><br/>

2.修改ParameterController.java

/**
* @RequestHeader("Host") 获取http请求头的 host信息
* @RequestHeader Map<String, String> header: 获取到http请求的所有信息
*@RequestHeader("accept") 获取http请求字段
*/
@GetMapping("/requestHeader")
public String requestHeader(@RequestHeader("host") String host,
                            @RequestHeader Map<String, String> header,
                            @RequestHeader("accept") String accept) {
     System.out.println("host-" + host);
     System.out.println("header-" + header);
     System.out.println("accept-" + accept);
     return "success";
}
测试

image-20230813081737549

image-20230813081827737

@RequestParam
代码实例

演示@RequestParam 使用,修改ParameterController.java , 完成测试
1.修改index.html

<a href="/hi?name=好运来&fruit=apple&fruit=pear&id=300&address=北京">@RequestParam-获取请求参数</a><br/><br/>

2.修改ParameterController.java

  /**
     * 解读
     * 如果我们希望将所有的请求参数的值都获取到,可以通过@RequestParam Map<String, String> paras 获取
     * 如果fruit 是多个值, 可以使用List 来接收
     * @RequestParam(value = "name") username 获取参数的值
     * @RequestParam的value接收的参数名要和表单一致
     * @RequestParam(value = "name") 对应的变量名可以自定义 如 username
     * @RequestParam Map<String, String> 可以获取所有的请求参数,但是参数是list集合只能获取一个
     */
    @GetMapping("/hi")
    public String hi(@RequestParam(value = "name") String username,
                     @RequestParam("fruit") List<String> fruits,
                     @RequestParam Map<String, String> paras) {

        System.out.println("username-" + username);
        System.out.println("fruit-" + fruits);
        System.out.println("paras-" + paras);
        return "success";
    }
测试

image-20230813082702535

image-20230813082723820

@CookieValue
代码实例

演示@CookieValue 使用,修改ParameterController.java , 完成测试
1.修改index.html

<a href="cookie">@CookieValue-获取cookie 值</a><br/><br/>

2.修改ParameterController.java

 /**
     * 因为我的浏览器目前没有cookie,我们可以自己设置cookie[技巧还是非常有用]
     * 如果要测试,可以先写一个方法,在浏览器创建对应的cookie
     * 说明 
     * 1. value = "cookie_key" 表示接收名字为 cookie_key的cookie
     * 2. 如果浏览器携带来对应的cookie , 那么 后面的参数是String ,则接收到的是对应的value
     * 3. 后面的参数是Cookie ,则接收到的是封装好的对应的cookie
     * 4. required = false表示该参数可以有也可以没有
     */
    @GetMapping("/cookie")
    public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                         HttpServletRequest request,
                         @CookieValue(value = "username", required = false) Cookie cookie) {
        System.out.println("cookie_value-" + cookie_value);
        if (cookie != null) {
            System.out.println("username-" + cookie.getName() + "-" + cookie.getValue());
        }
        System.out.println("-------------------------");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1.getName() + "=>" + cookie1.getValue());
        }
        return "success";
    }

测试

image-20230813083831792

image-20230813084025074

可以在浏览器手动创建cookie

image-20230813085428319

@RequestBody
代码示例

演示@RequestBody 使用,修改ParameterController.java , 完成测试
1.修改index.html

<hr/>
<h1>测试@RequestBody获取数据: 获取POST请求体</h1>
<form action="/save" method="post">
    姓名: <input name="name"/> <br>
    年龄: <input name="age"/> <br/>
    <input type="submit" value="提交"/>
</form>

2.修改ParameterController.java

 /**
     * @RequestBody 是整体取出Post请求内容
     */
    @PostMapping("/save")
    public String postMethod(@RequestBody String content) {
        System.out.println("content-" + content);
        return "success";
    }
测试

image-20230813093313834

image-20230813093345334

@RequestAttribute

演示@RequestAttribute 使用,创建com/nlc/web/controller/RequestController.java ,完成测试

代码示例

1.修改index.html

<a href="/login">@RequestAttribute-获取request域属性-</a>

2.创建RequestController.java

@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("user", "顺顺~");//向request域中添加的数据
        request.getSession().setAttribute("website", "http://www.baidu.com"); //向session中添加数据
        return "forward:/ok";  //请求转发到  /ok
    }

    @GetMapping("/ok")
    @ResponseBody
    public String ok(
            HttpServletRequest request,
            @SessionAttribute(value = "website", required = false) String website,
            @RequestAttribute(value = "user", required = false) String username
    ) 
    {
        //获取到request域中的数据
        System.out.println("username-" + username);
        System.out.println("通过servlet api 获取 username-" + request.getAttribute("user"));
        System.out.println("website-" + website);
        System.out.println("通过servlet api 获取 website-" + request.getSession().getAttribute("website"));
        return "success";
    }
}
测试

image-20230813093906651
image-20230813093943100

😄总结

  1. @RequestParam Map<String, String> 可以获取所有的请求参数,但如果参数是list集合只能获取一个。
  2. required = false表示请求时该参数可以有也可以没有。
  3. 测试时要注意是get还是post请求。

文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论😁
希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读🍻
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晨犀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值