spring boot 常见http请求url参数获取方法
作者:SealRui 时间:2023-01-26 13:59:43
在定义一个Rest接口时通常会利用GET、POST、PUT、DELETE来实现数据的增删改查;这几种方式有的需要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性
GET:一般用于查询数据,采用明文进行传输,一般用来获取一些无关用户信息的数据
POST:一般用于插入数据
PUT:一般用于数据更新
DELETE:一般用于数据删除;一般都是进行逻辑删除(即:仅仅改变记录的状态,而并非真正的删除数据)
1、@PathVaribale 获取url中的数据
请求URL:localhost:8080/hello/id 获取id值
实现代码如下:
@RestController
publicclass HelloController {
@RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
return"id:"+id+" name:"+name;
}
}
在浏览器中 输入地址:
localhost:8080/hello/100/hello
输出:
id:81name:hello
2、@RequestParam 获取请求参数的值
获取url参数值,默认方式,需要方法参数名称和url参数保持一致
请求URL:localhost:8080/hello?id=1000
@RestController
publicclass HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam Integer id){
return"id:"+id;
}
}
输出:
id:100
url中有多个参数时,如:
localhost:8080/hello?id=98&&name=helloworld
具体代码如下:
@RestController
publicclass HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam Integer id,@RequestParam String name){
return"id:"+id+ " name:"+name;
}
}
获取url参数值,执行参数名称方式
localhost:8080/hello?userId=1000
@RestController
publicclass HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam("userId") Integer id){
return"id:"+id;
}
}
输出:
id:100
来源:https://www.cnblogs.com/SealRui/archive/2021/03/03/14475114.html
标签:spring,boot,http请求,url参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java之CSV大批量数据入库的实现
2021-06-30 15:53:49
![](https://img.aspxhome.com/file/2023/0/66170_0s.png)
Java实现的RSA加密解密算法示例
2022-05-03 09:20:38
Android编程之canvas绘制各种图形(点,直线,弧,圆,椭圆,文字,矩形,多边形,曲线,圆角矩形)
2023-10-16 12:16:14
![](https://img.aspxhome.com/file/2023/6/137336_0s.png)
Android应用获取设备序列号的方法
2021-07-17 00:55:31
java面向对象编程类的内聚性分析
2022-02-24 23:43:00
Android关于FTP文件上传和下载功能详解
2021-06-02 18:15:55
IOS 实现摇一摇的操作
2023-07-02 13:46:53
Android仿微信二维码和条形码
2021-09-02 03:46:22
![](https://img.aspxhome.com/file/2023/9/101849_0s.jpg)
Java计算文本MD5加密值的方法示例
2023-11-15 13:18:48
C#日历样式的下拉式计算器实例讲解
2022-05-07 20:11:33
![](https://img.aspxhome.com/file/2023/4/85724_0s.jpg)
Java日志框架之logback使用详解
2022-02-18 05:33:47
Android实现图片上传蒙层进度条
2022-05-06 04:35:43
c#中Winform实现多线程异步更新UI(进度及状态信息)
2022-08-26 05:54:40
![](https://img.aspxhome.com/file/2023/6/96376_0s.jpg)
Android编程之高效开发App的10个建议
2021-08-28 15:55:46
在IDEA中 实现给main方法附带参数的操作
2022-10-23 14:42:03
![](https://img.aspxhome.com/file/2023/7/71237_0s.jpg)
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
Android 偷拍功能实现(手机关闭依然拍照)详解及实例代码
2023-01-08 00:23:19
![](https://img.aspxhome.com/file/2023/9/114289_0s.png)
java实现单链表、双向链表
2023-02-09 03:15:59
Android动态添加碎片代码实例
2023-10-19 23:35:13
C# WinForm打开PDF文件并在窗体中显示
2023-11-14 10:47:49