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的时候,发现文档里都描述的不太清楚,最后终于琢磨出来了,分享给有需要的朋友 ...
随机推荐
- thinkphp模版主题使用方法
3.1.3模版主题使用方法,手册貌似没有.配置项: 'DEFAULT_THEME'=>'default',//默认主题 'THEME_LIST'=>'default,theme',//主题 ...
- in mind (不是 切记 的意思)
Both Grunt and Gulp.js perform these automation tasks particularly well, although Gulp.js has the ed ...
- java dom4j 解析xml使用实践
参考:https://dom4j.github.io/ http://www.cnblogs.com/liuling/archive/2013/02/05/dom4jxml.html 常用api: 1 ...
- iptables之精髓(二)
iptables实际操作 使用-v选项后,iptables为我们展示的信息更多了,那么,这些字段都是什么意思呢?我们来总结一下 pkts:对应规则匹配到的报文的个数. bytes:对应匹配到的报文包的 ...
- Pandas与Matplotlib结合进行可视化
前面所介绍的都是以表格的形式中展现数据, 下面将介绍Pandas与Matplotlib配合绘制出折线图, 散点图, 饼图, 柱形图, 直方图等五大基本图形. Matplotlib是python中的一个 ...
- jenkins:多个job时怎么按照一定顺序执行构建
一.安装Jenkins多项目构建插件(我已安装):Multijob 二.新建Multijob Project任务 三.配置
- 【ABAP系列】SAP ABAP 高级业务应用程序编程(ABAP)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 高级业务应用程 ...
- fatal error: nvcuvid.h: No such file
https://www.cnblogs.com/rabbull/p/11154997.html
- HDU3191 【输出次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3191 How Many Paths Are There Time Limit: 2000/1000 M ...
- 主成分分析(PCA)与线性判别分析(LDA)
主成分分析 线性.非监督.全局的降维算法 PCA最大方差理论 出发点:在信号处理领域,信号具有较大方差,噪声具有较小方差 目标:最大化投影方差,让数据在主投影方向上方差最大 PCA的求解方法: 对样本 ...