# Spring Boot課程筆記4: 參數處理 ## @PathVariable ### 多於GET請求時使用(搜尋指定物件) * **定義方法** -透過@GetMapping注解定義該方法,並映射相關URL請求 http://localhost:8080/aip/v1/books/{id} -於方法中的參數透過@PathVariable定義URL所帶入的參數,如範例程式中getOne()所示如此才會接收到URL所帶入的值 ``` @RestController //@Controller @RequestMapping("/api/v1") public class HelloController { //@RequestMapping(value = "/say", method = RequestMethod.GET) @PostMapping("/say") public String hello() { return "Hello Spring Boot"; } @GetMapping("/books") //@ResponseBody public Object getAll() { Map<String, Object> map = new HashMap<>(); map.put("name", "hello"); map.put("age", 18); return map; } @GetMapping("/books/{id}") public Object getOne(@PathVariable long id) { Map<String, Object> book = new HashMap<>(); book.put("name", "三國演義"); book.put("isbn", "0800092000"); book.put("author", "諸葛亮"); return book; } } ``` -帶入的參數Spring Boot會自動幫你轉換成方法中你所定義的型別 -URL中定義參數名需與方法中參數名相同,如需有例外則需在@PathVariable中定義欲映射的URL參數,參考範例程式 ``` @GetMapping("/books/{id}") public Object getOne(@PathVariable("id") long bid) { Map<String, Object> book = new HashMap<>(); book.put("name", "三國演義"); book.put("isbn", "0800092000"); book.put("author", "諸葛亮"); return book; } ``` -如遇複數參數需求也可直接於URL中新增,並於程式內透過注解定義即可。如遇特殊需求可用正則表達式進行簡單處理,範例程式如下 ``` /** * 正則表達式: {參數名: 正則表達式} * @param id * @param username * @return */ @GetMapping("/books/{id}/{username:[a-z_]+}") public Object getOne(@PathVariable long id, @PathVariable String username) { Map<String, Object> book = new HashMap<>(); book.put("name", "三國演義"); book.put("isbn", "0800092000"); book.put("author", "諸葛亮"); book.put("username", username); return book; } ``` ## @RequestParam ### 多於POST請求時使用(傳送表單) * **定義方法** -透過@PostMapping注解定義該方法,並映射相關URI請求 http://localhost:8080/aip/v1/books -於方法中的參數透過@RequestParam定義URI所帶入的參數,如範例程式中post()所示如此才會接收到URI所帶入的值 ``` @PostMapping("/books") public Object post(@RequestParam("name") String name, @RequestParam("author") String author, @RequestParam("isbn") String isbn) { Map<String, Object> book = new HashMap<String, Object>(); book.put("name", name); book.put("author", author); book.put("isbn", isbn); return book; } ``` ## 模擬需求情境: 定義分頁頁數與每頁能列出之數量。 * **透過URI中定義參數並帶入後端 http://localhost:8080/api/v1/books?page=1&size=10 ?表示參數開頭,參數之間透過 & 連結** -選用GET請求,並於後端透過@RequestParam獲取參數進行處理,參考範例程式 ``` @GetMapping("/books") //@ResponseBody public Object getAll(@RequestParam("page") int page, @RequestParam("size") int size) { Map<String, Object> book = new HashMap<String, Object>(); book.put("name", "三國演義"); book.put("isbn", "0800092000"); book.put("author", "諸葛亮"); Map<String, Object> book2 = new HashMap<String, Object>(); book2.put("name", "西遊記"); book2.put("isbn", "020488995"); book2.put("author", "孫悟空"); List<Map> contents = new ArrayList<>(); contents.add(book); contents.add(book2); Map<String, Object> pageMap = new HashMap<String, Object>(); pageMap.put("page", page); pageMap.put("size", size); pageMap.put("content", contents); return pageMap; } ``` -PostMan參考 ![](https://i.imgur.com/DTJqFa4.jpg) -也可於參數中透過defaultValue預設默認值,如範例(方法內容省略) ``` public Object getAll(@RequestParam("page") int page, @RequestParam(value = "size", defaultValue = "10") int size) ``` ## 結語 * @PathVariable: 多用於刪除或獲取單一資源 * @RequestParam: 多用於提交/ 新增表單,或透過URI中的參數過濾查詢並回傳 [補充討論: URI與URL區別](https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/53793/) [參考課程Reference](https://www.udemy.com/course/spring-boot-u/) ###### tags: `Spring Boot`