SpringBoot如何接收Post请求Body里面的参数
作者:赛尔号- 时间:2023-07-30 13:43:35
如何接收Post请求Body里的参数
ApiPost测试数据
{
"list": [
"{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
"{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
"{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}",
"{'time':'xxxxx','distinct_id':'xxxx','appId':'xxxx'}"
],
"type": 1
}
Java接收数据
需要提前创建好对应的Bean
由于传递过来的数据是String类型,因此需要转换一步
import cn.hutool.json.JSONObject;
@PostMapping("/data/callback")
public Object testResponse(
@RequestBody JSONObject jsonObject
) {
JSONArray jsonList = jsonObject.getJSONArray("list");
ArrayList<DataEntity> list = new ArrayList<>();
for (Object jsObject : jsonList){
DataEntity dataEntity = JSONObject.parseObject(jsObject.toString(), DataEntity.class);
list.add(dataEntity);
}
Integer type = (Integer) jsonObject.get("type");
log.info(String.format("本次共接收%d条数据,type=%d",list.size(),type));
for (DataEntity dataEntity : list) {
log.info(dataEntity.toString());
}
}
SpringBoot获取参数常用方式
参数在body体中
在方法形参列表中添加@RequestBody注解
@RequestBody 作用是将请求体中的Json字符串自动接收并且封装为实体。如下:
@PostMapping("/queryCityEntityById")
public Object queryCityEntityById(@RequestBody CityEntity cityEntity)
{
return ResultUtil.returnSuccess(cityService.queryCityById(cityEntity.getId()));
}
PathVaribale获取url路径的数据
如下:
@RestController
public class 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;
}
}
RequestParam获取请求参数的值
获取url参数值,默认方式,需要方法参数名称和url参数保持一致
localhost:8080/hello?id=1000,如下:
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam Integer id){
return "id:"+id;
}
}
来源:https://blog.csdn.net/qq_36811160/article/details/111362637
标签:SpringBoot,Post请求,Body,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java多线程 自定义线程池详情
2022-12-02 09:22:08
Java中JDBC连接数据库详解
2023-08-10 16:57:18
![](https://img.aspxhome.com/file/2023/2/58162_0s.png)
Android实现ListView数据动态加载的方法
2021-11-02 23:01:02
springboot使用Mybatis-plus分页插件的案例详解
2023-10-27 13:47:43
Android开发手册TextInputLayout样式使用示例
2023-10-16 11:03:12
Java用单向环形链表来解决约瑟夫环Josepfu问题
2023-03-13 18:54:20
![](https://img.aspxhome.com/file/2023/9/81339_0s.png)
Java SE之了解泛型
2022-08-10 08:42:54
![](https://img.aspxhome.com/file/2023/8/84788_0s.jpg)
Android中使用Handler及Countdowntimer实现包含倒计时的闪屏页面
2023-01-21 19:05:08
![](https://img.aspxhome.com/file/2023/8/125398_0s.gif)
android操作XML的几种方法总结
2021-08-10 06:06:16
Android 中Notification弹出通知实现代码
2021-05-31 14:48:18
java @Value(
2023-10-05 02:54:47
![](https://img.aspxhome.com/file/2023/7/63017_0s.png)
浅谈java 单例模式DCL的缺陷及单例的正确写法
2022-04-14 05:58:23
![](https://img.aspxhome.com/file/2023/5/76615_0s.jpg)
IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页
2023-04-14 18:33:46
![](https://img.aspxhome.com/file/2023/7/71157_0s.jpg)
Java URL自定义私有网络协议
2021-08-11 02:21:06
![](https://img.aspxhome.com/file/2023/1/85871_0s.png)
C#解析JSON实例
2022-05-08 16:09:56
IntelliJ IDEA的代码搁置功能实现
2022-03-28 07:32:52
![](https://img.aspxhome.com/file/2023/1/74921_0s.png)
UGUI ScrollRect滑动定位优化详解
2021-12-06 09:56:13
![](https://img.aspxhome.com/file/2023/5/94605_0s.jpg)
浅谈在Spring中如何使用数据源(DBCP、C3P0、JNDI)
2022-12-14 18:11:58
Android 动态菜单实现实例代码
2023-05-19 16:24:58
![](https://img.aspxhome.com/file/2023/4/117094_0s.gif)
Spring Security 图片验证码功能的实例代码
2023-11-17 14:23:56
![](https://img.aspxhome.com/file/2023/0/72350_0s.jpg)