SpringBoot 2 系列八:Web开发——请求参数处理1

本文详细探讨了SpringBoot 2中处理请求参数的各种方式,包括REST风格的URL参数、请求头、请求参数、Cookie、请求体、请求域数据以及矩阵变量的获取。并解释了请求参数处理的原理,涉及处理器映射器、适配器和参数解析器的角色。同时,指出了SpringBoot默认关闭矩阵变量功能,并提供了开启该功能的两种方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求参数处理

REST风格, url 传参,@PathVariable 接参

请求url http://127.0.0.1:8080/test1/1
controller

@Controller
public class TestController {
   
   
    @ResponseBody
    @RequestMapping("/test1/{id}")
    public Map<String,Object> test1(@PathVariable("id") Integer id){
   
   
        HashMap<String, Object> result = new HashMap<>();
        result.put("id",id);
        return result;
    }
}

请求 url http://127.0.0.1:8080/test2/1/hobby/ball
controller

@Controller
public class TestController {
   
   

    @ResponseBody
    @RequestMapping("/test2/{id}/hobby/{hobbyName}")
    public Map<String,Object> test2(@PathVariable("id") Integer id,@PathVariable("hobbyName") String hobbyName){
   
   
        HashMap<String, Object> result = new HashMap<>();
        result.put("id",id);
        result.put("hobbyName",hobbyName);
        return result;
    }
}

使用 Map<String,String>接收请求参数
请求 url http://127.0.0.1:8080/test3/1/hobby/ball
controller

@Controller
public class TestController {
   
   

    @ResponseBody
    @RequestMapping("/test3/{id}/hobby/{hobbyName}")
    public Map<String,String> test3(@PathVariable Map<String,String> params){
   
   
        return params;
    }

}

获取请求头 @RequestHeader

获取单个请求头信息
请求 url http://127.0.0.1:8080/test4/getHeader
controller

    @ResponseBody
    @RequestMapping("/test4/getHeader")
    public String test4(@RequestHeader("User-Agent") String useragent){
   
   
        return useragent;
    }

使用 Map<String,String> 获取所有的请求头信息
请求 url http://127.0.0.1:8080/test5/getHeader
controller

    @ResponseBody
    @RequestMapping("/test5/getHeader")
    public Map<String ,String> test5(@RequestHeader Map<String ,String> headers){
   
   
        return headers;
    }

接收请求参数 @RequestParam

逐一接收请求参数
请求 url http://127.0.0.1:8080/test6/getParam?userId=1&password=123456
controller

   @ResponseBody
    @RequestMapping("/test6/getParam")
    public Map<String ,Object> test6(@RequestParam("userId") String userId,@RequestParam("password") String password){
   
   
        Map<String, Object> result = new HashMap<>();
        result.put("userId",userId);
        result.put("password",password);
        return result;
    }

使用 Map<String,String> 封装请求参数
请求 url http://127.0.0.1:8080/test7/getParam?userId=1&password=123456
controller

    @ResponseBody
    @RequestMapping("/test7/getParam")
    public Map<String ,String> test7(@RequestParam Map<String,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秋水浮萍任缥缈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值