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。