都是以前的筆記了,有時間就整理出來了,SpringBoot接收前端參數的三種方法,首先第一種代碼:
@RestController public class ControllerTest { //訪問路徑:http://localhost:8080/hello/5 @GetMapping(value = "/hello/{id}") public String hello(@PathVariable("id") Integer id){ return "ID:" + id; } }
第二種:
可以通過注解@RequestParam("xxx")來綁定指定的參數,在這里聲明的參數為ids,但是@RequestParam()注解中確實id,這個時候前端傳的值就應該為id,如果前台傳送的是ids的話,在這里是接收不到的。
@RestController public class ControllerTest { //訪問路徑:http://localhost:8080/hello?id=1551 @GetMapping(value = "/hello") public String hello(@RequestParam("id") Integer ids){ return "ID:" + id; } }
第三種像這種,如果傳進來的參數為空的話,可以設置為默認的:
//訪問路徑:http://localhost:8080/hello?id=1551 @GetMapping(value = "/hello") public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){ return "ID:" + id; }