下面為7種服務端獲取前端傳過來的參數的方法
1、直接把表單的參數寫在Controller相應的方法的形參中,適用於GET 和 POST請求方式
這種方式不會校驗請求里是否帶參數,即下面的username和password不帶也會響應成功
@RestController @RequestMapping("/tools") public class InnerController {
@RequestMapping("/addUser1") public String addUser1(String username,String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "success"; } }
測試代碼
POST請求方式
<script> var xhr = new XMLHttpRequest() xhr.open('POST', 'http://localhost:8080/tools/addUser1') // 設置請求行 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') xhr.send('username=zhangsan&password=123') // 以 urlencoded 格式設置請求體 xhr.onload=function(){ if(xhr.readyState!==4) return console.log(xhr.responseText) } </script>
GET請求方式:
<script> var xhr = new XMLHttpRequest() xhr.open('GET', 'http://localhost:8080/tools/addUser1?username=zhangsan&password=123') // 設置請求行 xhr.send() xhr.onload=function(){ if(xhr.readyState!==4) return console.log(xhr.responseText) } </script>
2、通過HttpServletRequest接收,適用於GET 和 POST請求方式
通過HttpServletRequest對象獲取請求參數
@RestController @RequestMapping("/tools") public class InnerController {
@RequestMapping("/addUser2") public String addUser2(HttpServletRequest request) { String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "success"; } }
測試代碼同上
3、通過一個bean來接收,適用於GET 和 POST請求方式
(1)建立一個和表單中參數對應的bean
@Data @Builder @AllArgsConstructor @NoArgsConstructor public class DemoUser { private String username; private String password; }
(2)用這個bean來封裝接收的參數
@RestController @RequestMapping("/tools") public class InnerController {
@RequestMapping("/addUser3") public String addUser3(DemoUser user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "success"; } }
測試代碼同上
4、通過@PathVariable獲取路徑中的參數,適用於GET請求
通過注解獲取url參數
@RestController @RequestMapping("/tools") public class InnerController {
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET) public String addUser4(@PathVariable String username,@PathVariable String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "success";
} }
測試代碼
<script>
var xhr = new XMLHttpRequest()
xhr.open('GET', 'http://localhost:8080/tools/addUser4/username=zhangsan/password=123') // 設置請求行
xhr.send()
xhr.onload=function(){
if(xhr.readyState!==4) return
console.log(xhr.responseText)
}
</script>
自動將URL中模板變量{username}和{password}綁定到通過@PathVariable注解的同名參數上,即入參后username=zhangsan、password=123
5、使用@ModelAttribute注解獲取參數,適用於POST請求
@RestController @RequestMapping("/tools") public class InnerController { @RequestMapping(value="/addUser5",method=RequestMethod.POST) public String addUser5(@ModelAttribute("user") DemoUser user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "success"; } }
測試代碼
<script>
var xhr = new XMLHttpRequest()
xhr.open('POST', 'http://localhost:8080/tools/addUser5') // 設置請求行
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send('username=zhangsan&password=123')
xhr.onload=function(){
if(xhr.readyState!==4) return
console.log(xhr.responseText)
}
</script>
6、用注解@RequestParam綁定請求參數到方法入參,適用於GET 和 POST請求方式
添加@RequestParam注解,默認會校驗入參,如果請求不帶入參則會報錯,可以通過設置屬性required=false解決,例如: @RequestParam(value="username", required=false) ,這樣就不會校驗入參,於第一種請求方式一樣
@RestController @RequestMapping("/tools") public class InnerController { @RequestMapping(value="/addUser6",method=RequestMethod.GET) public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "success"; } }
測試代碼同上
7、用注解@RequestBody綁定請求參數到方法入參 , 用於POST請求
@RequestBody主要用來接收前端傳遞給后端的json字符串中的數據的(請求體中的數據的)
@RestController @RequestMapping("/tools") public class InnerController { @RequestMapping(value="/addUser7",method=RequestMethod.POST) public String addUser7(@RequestBody DemoUser user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "success"; } }
測試代碼: 請求頭需要指定為json類型
<script>
var xhr = new XMLHttpRequest()
xhr.open('POST', 'http://localhost:8080/tools/addUser7') // 設置請求行
xhr.setRequestHeader('Content-Type','application/json')
xhr.send('{"username":"zhangsan","password":"123"}')
xhr.onload=function(){
if(xhr.readyState!==4) return
console.log(xhr.responseText)
}
</script>
DemoUser這個類為一個實體類,里面定義的屬性與URL傳過來的屬性名一一對應。
