下面为7种服务端获取前端传过来的参数的方法
1、直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式
@RestController
@RequestMapping("/tools")public classInnerController {
@RequestMapping("/addUser1")publicString addUser1(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);return "success";
}
}
测试代码
POST请求方式
var xhr = newXMLHttpRequest()
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.οnlοad=function(){if(xhr.readyState!==4) return console.log(xhr.responseText)
}
GET请求方式:
var xhr = newXMLHttpRequest()
xhr.open('GET', 'http://localhost:8080/tools/addUser1?username=zhangsan&password=123') //设置请求行
xhr.send()
xhr.οnlοad=function(){if(xhr.readyState!==4) return console.log(xhr.responseText)
}
2、通过HttpServletRequest接收,适用于GET 和 POST请求方式。
@RestController
@RequestMapping("/tools")public classInnerController {
@RequestMapping("/addUser2")publicString 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
@NoArgsConstructorpublic classDemoUser {privateString username;privateString password;
}
(2)用这个bean来封装接收的参数
@RestController
@RequestMapping("/tools")public classInnerController {
@RequestMapping("/addUser3")publicString addUser3(DemoUser user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());return "success";
}
}
测试代码同上
4、通过@PathVariable获取路径中的参数,适用于GET请求
@RestController
@RequestMapping("/tools")public classInnerController {
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)publicString addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);return "success";
}
}
测试代码
var xhr = newXMLHttpRequest()
xhr.open('GET', 'http://localhost:8080/tools/addUser4/username=zhangsan/password=123') //设置请求行
xhr.send()
xhr.οnlοad=function(){if(xhr.readyState!==4) return console.log(xhr.responseText)
}
自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=zhangsan、password=123
5、使用@ModelAttribute注解获取参数,适用于POST请求
@RestController
@RequestMapping("/tools")public classInnerController {
@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";
}
}
测试代码
var xhr = newXMLHttpRequest()
xhr.open('POST', 'http://localhost:8080/tools/addUser5') //设置请求行
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send('username=zhangsan&password=123')
xhr.οnlοad=function(){if(xhr.readyState!==4) return console.log(xhr.responseText)
}
6、用注解@RequestParam绑定请求参数到方法入参
当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)
@RestController
@RequestMapping("/tools")public classInnerController {
@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请求
@RestController
@RequestMapping("/tools")public classInnerController {
@RequestMapping(value="/addUser7",method=RequestMethod.POST)publicString addUser7(@RequestBody DemoUser user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());return "success";
}
}
测试代码: 请求头需要指定为json类型
var xhr = newXMLHttpRequest()
xhr.open('POST', 'http://localhost:8080/tools/addUser7') //设置请求行
xhr.setRequestHeader('Content-Type','application/json')
xhr.send('{"username":"zhangsan","password":"123"}')
xhr.οnlοad=function(){if(xhr.readyState!==4) returnconsole.log(xhr.responseText)
}
DemoUser这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。