SpringBoot普通参数与基本注解(@PathVariable、@RequestHeader、@RequestParam、@RequestBody)
@PathVariable(获取路径变量)
index.html: 基本注解测试测试基本注解:
@PathVariable(路径变量):
@RequestParam(获取请求参数)
@RequestHeader(获取请求头)
@CookieValue(获取cookie值)
@RequestBody(获取请求体[POST])
ParameterTestController类:@RestController public class ParameterTestController {@GetMapping("user/{id}/pet/{petName}")//映射html中的链接地址——href="user/3/pet/tom" public Map getUser(@PathVariable() String id,//获取路径变量id的值 @PathVariable() String petName, @PathVariable Map pv){//获取链接中全部路径变量 Map map=new HashMap<>(); map.put("id",id); map.put("petName",petName); map.put("pv",pv); return map; } }
结果:

@RequestParam(获取请求参数)
index.html@RequestParam(获取请求参数)
ParameterTestController类添加: @GetMapping("user/zhangsan")//映射html中的链接地址——href="user/zhangsan?age=18&interests=basketball&interests=game" public Map getUser2(@RequestParam("age") Integer age, @RequestParam("interests") List interests, @RequestParam Map params){Map map2=new HashMap<>(); map2.put("age",age); map2.put("interests",interests); map2.put("params",params); return map2; }
结果:

@RequestHeader(获取请求头)
index. html:@RequestHeader(获取请求头)
ParameterTestController类添加: @GetMapping("user/RequestHeader") public Map getUser3(@RequestHeader("Accept") String Accept, @RequestHeader Map header){Map map=new HashMap<>(); map.put("Accept",Accept); map.put("header",header); return map;
结果:

@RequestBody(获取请求体[POST])
index.html 测试@RequestBody获取数据
用户名:
邮箱:
ParameterTestController类添加:@PostMapping("/save") public Map postMethod(@RequestBody String content){Map map = new HashMap<>(); map.put("content",content); return map; }
结果:

来源:https://www.icode9.com/content-4-885401.html
