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

请求参数处理

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,String> para
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秋水浮萍任缥缈

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

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

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

打赏作者

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

抵扣说明:

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

余额充值