一、请求路径参数
1、@PathVariable
@GetMapping("/demo/{id}")
public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
System.out.println("id="+id);
System.out.println("name="+name);
}
2、@RequestParam
请求路径参数,顾名思义参数再请求路径的后面:http://127.0.0.1:8080/account/findJudicialAuctionDemo?province=山东省&city=连云港市,一般为get请求,不建议使用,容易泄露信息
@RequestMapping(value = '/findJudicialAuctionDemo',method = RequestMethod.GET)
String findJudicialAuctionDemo(@RequestParam(value = "province",required = false)String province,
@RequestParam(value = "city",required = false)String city){
System.out.println(province));
}
required 表示是否可以为空,false表示可以不输入
示例测试使用postman
二、body参数
映射成对象,需要建立一个类对象,映射到对象中
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Person person) {
System.out.println(person.toString());
}
映射成map
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Map<String, String> message) {
System.out.println(message.get("name"));
System.out.println(message.get("age"));
}
三、请求头参数以及cookie
暂时没遇到,照抄一下别人的
@GetMapping("/demo3") public void demo3(HttpServletRequest request) {
System.out.println(request.getHeader("myHeader"));
for (Cookie cookie : request.getCookies()) {
if ("myCookie".equals(cookie.getName())) {
System.out.println(cookie.getValue());
}
}
}
@GetMapping("/demo3")
public void demo3(@RequestHeader(name = "myHeader") String myHeader, @CookieValue(name = "myCookie") String myCookie) {
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
}