@PathVariable(獲取路徑變量) index.html: 基本注解測(cè)試測(cè)試基本注解: @PathVariable(路徑變量): @RequestParam(獲取請(qǐng)求參數(shù)) @RequestHeader(獲取請(qǐng)求頭) @CookieValue(獲取cookie值) @RequestBody(獲取請(qǐng)求體[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; } } 結(jié)果: @RequestParam(獲取請(qǐng)求參數(shù)) index.html@RequestParam(獲取請(qǐng)求參數(shù)) 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; } 結(jié)果: @RequestHeader(獲取請(qǐng)求頭) index. html:@RequestHeader(獲取請(qǐng)求頭) 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; 結(jié)果: @RequestBody(獲取請(qǐng)求體[POST]) index.html 測(cè)試@RequestBody獲取數(shù)據(jù) 用戶名: 郵箱: ParameterTestController類添加:@PostMapping("/save") public Map postMethod(@RequestBody String content){Map map = new HashMap<>(); map.put("content",content); return map; } 結(jié)果: 來源:https://www./content-4-885401.html |
|