web开发-参数请求(基本注解)


https://www.yuque.com/atguigu/springboot/rmxq85 >web学习


普通参数与基本注解

注解:

@PathVariable 路径参数 url输入:/user/01/abc

@RequestHeader 获得请求头 使用这个后页面会显示标头

@ModelAttribute 传递的全部封装成对象

@RequestParam 请求参数

@RequestBody 请求体,请求的一些内容

@MatrixVariable、@CookieValue

创建一个UserController

    @RequestMapping("/user/{id}/{username}")
    public Map<String, Object> queryUsers(@PathVariable("id") String id,
                                          @PathVariable("username") String username,
                                          @RequestHeader("User-Agent") String userAgent,
                                          @RequestHeader Map<String,String> header,
                                          @RequestParam("age") int age,
                                          @RequestParam("hometown" ) List<String> hometowns
                                          
    ){
        Map<String,Object> map = new HashMap<>();
        map.put("id", id);
        map.put("username", username);
        map.put("userAgent", userAgent);
        // map.put("header" , header);
        map.put("age", age);
        map.put("hometown", hometowns);
        return map;
    }

@PathVariable 拿到路径上的 id 。匹配的是路径,路径上的参数。

HTTP状态码

https://www.runoob.com/http/http-status-codes.html
在这里插入图片描述

请求标头

Accept:能接受什么样类型的请求

Host:访问路径

User-Agent:浏览器用户代理

测试RequestBody

    @PostMapping(value = "/getUser")
    public Map<String, Object> getUser(@RequestBody String content){
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }
<h1>
    测试requestbody
</h1>
<form action="/getUser" method="post">
    姓名:<input name="username" /> <br>
    年龄:<input name="age" /> <br>
    <input type="submit" value="测试requestbody提交" />
</form>

访问页面
在这里插入图片描述
在这里插入图片描述
注意:默认是 @PostMapping 换成 @GetMapping 会报错。

@RequestMapping改成@GetMapping

如果想用@GetMapping

可以使用参数的方式传,用@RequestParam

    @GetMapping(value = "/getUser")
    public Map<String, Object> getUser(@RequestParam String username,
                                       @RequestParam int age
    ){
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("age", age);
        return map;
    }

注:使用 @GetMapping 用户信息会在地址栏上,尽量使用 @PostMapping。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值