springboot 服务端获取前端传过来的参数7种方式
下面为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传过来的属性名一一对应。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!