spring boot中post请求接收参数

在我们定义接口时通常会用到post请求,对于post请求我们如何接收从页面传过来的参数
以下总结了几种方法,适用于大多数类型

1.对于参数类型不确定的一个或多个参数类型

当我们post请求只有一两个参数时,不需要创建对象时,可以使用JSONObject实体类。

  1. 如下用post传递Integer类型,在controller层定义**getInteger()**方法;
    @ResponseBody
    @PostMapping("/postID")
    public Integer getInteger(@RequestBody JSONObject jsonObject){
    String a=jsonObject.get(“id”).toString();
    Integer id=Integer.parseInt(a);
    return id;
    }
    2.用postman测试如图;
    spring boot中post请求接收参数_第1张图片
    传递String类型同样也是这样

3.当我们传递两个多个参数时;
@ResponseBody
@PostMapping("/idandstring")
public String getInteger(@RequestBody JSONObject jsonObject){
String code=jsonObject.get(“code”).toString();
String a=jsonObject.get(“id”).toString();
Integer id=Integer.parseInt(a);
return “数字:”+id+“字符串:”+code;
}
用postman测试如图;
spring boot中post请求接收参数_第2张图片

对象参数传递

对象参数传递比较简单springboot会自动将参数封装给对象,这里不做说明

你可能感兴趣的:(spring boot中post请求接收参数)