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传过来的属性名一一对应。
springboot 服务端获取前端传过来的参数7种方式的更多相关文章
- react native android 上传文件,Nodejs服务端获取上传的文件
React Native端 使用react-native-image-picker 做出选择图片的操作,选择完成后,直接将图片Post至服务器,保存在服务器的某个地方(保存图片的路径需要公开显示),并 ...
- spring-boot如何去获取前端传递的参数
本文主要讨论spring-boot如何获取前端传过来的参数,这些参数主要有两大类,一类是URL里的参数,一个是请求body里的参数 url里的参数 通过url里传过来的参数一般有三种方式,下面我们来看 ...
- IE8下服务端获取客户端文件的路径为C:/fakePath问题的解决方案
上一篇文章上提到,IE8下服务端获取客户端文件的路径时,会变成C:/fakePath问题,于是乎通过文件路径去获得文件大小就失败了. 上网搜了一下,主要原因是IE8因为安全考虑,在上传文件时屏蔽了真实 ...
- spring集成webSocket实现服务端向前端推送消息
原文:https://blog.csdn.net/ya_nuo/article/details/79612158 spring集成webSocket实现服务端向前端推送消息 1.前端连接webso ...
- ftpget 从Windows FTP服务端获取文件
/********************************************************************************* * ftpget 从Windows ...
- Android从服务端获取json解析显示在客户端上面
Android从服务端获取json解析显示在客户端上面 百度经验:jingyan.baidu.com 首先说一下Json数据的最基本的特点,Json数据是一系列的键值对的集合,和XML数据来比,Jso ...
- 服务端获取客户端html页面内容-2013-6-28-2
客户端怎么提交 整个html页面? 分析: 1>我们知道b/s模式,也知道http协议.服务端想要获取客户端的数据,客户端就 必须提交给它,服务器才能获取到. 2> ...
- MIME类型-服务端验证上传文件的类型
MIME的作用 : 使客户端软件,区分不同种类的数据,例如web浏览器就是通过MIME类型来判断文件是GIF图片,还是可打印的PostScript文件. web服务器使用MIME来说明发送数据的种类, ...
- 解决有关flask-socketio中服务端和客户端回调函数callback参数的问题(全网最全)
由于工作当中需要用的flask_socketio,所以自己学习了一下如何使用,查阅了有关文档,当看到回调函数callback的时候,发现文档里都描述的不太清楚,最后终于琢磨出来了,分享给有需要的朋友 ...
随机推荐
- <JavaScript>可枚举属性与不可枚举属性
在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...
- kvm管理工具Webvirtmgr安装
虚拟机版本vmware workstation 15.5.0 pro (也就是linux版) cat /etc/redhat-release CentOS Linux release 7.4.17 ...
- jeecg启动报错“com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.”的解决办法
在运行"maven build"-->"tomcat:run"之后,报如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQ ...
- 【leetcode】500_Keyboard Row
problem 500. Keyboard Row 题意:判断给出的某个单词是否可以使用键盘上的某一行字母type得到: 注意大小写的转换: solution1: 使用set保存三行字符,查看每个字符 ...
- Scrapy框架学习参考资料
00.Python网络爬虫第三弹<爬取get请求的页面数据> 01.jupyter环境安装 02.Python网络爬虫第二弹<http和https协议> 03.Python网络 ...
- PHP常用正则表达式精选
$regex = '[\u4e00-\u9fa5]'; //匹配中文字符的正则表达式 $regex = '^[\u4E00-\u9FA5A-Za-z0-9]+$'; or $regex = '^[\u ...
- libvirt2.0安装
目录 1.libvirt介绍 2.卸载系统自带的libvirt 2.1.查看当前安装的libvirt相关包 2.2.全部卸载掉 3.使用tar包编译安装 3.1.解压缩 3.2.生成Makefile文 ...
- Eclipse配置编写HTML/JS/CSS/JSP页面的自动提示
我们平时用eclipse开发jsp页面时智能提示效果不太理想,今天用了两个小时发现了eclipse也可以像Visual Studio 2008那样完全智能提示HTML/JS/CSS代码,使用eclip ...
- Nginx反向代理简单配置
一.首先在IIS中部署两个站点,localhost:86 .localhost:5000 二.修改C:\windows\system32\drivers\etc\hosts文件,增加 127.0.0. ...
- js函数(2)
8.3函数的形参和实参 js中的函数并未指定函数形参的类型,函数调用也未对传入的实参值做任何类型的检查. 8.3.1函数的形参和实参 当调用函数时传入的实参比函数声明时指定的形参个数要少,剩下的参数都 ...