栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)

一、 访问请求、URI、URL、请求参数 四者的区别

一个完整的请求路径:http://www.test.com/user/031267/view?username=zhangsan&age=20

请求URL (requestURL)= http://www.test.com/user/031267/view
请求URI (requestURI)= /user/031267/view
请求参数(queryString)= username=zhangsan&age=20

二、注解区别

1、@PathVariable: 映射 URL 绑定的占位符, 能使传过来的参数绑定到路由上。

@PathVariable 可以将 URL 中 占位符参数 绑定到控制器处理 方法的入参 中:
URL 中的 { xxx } 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。

请求路径 : http://www.test.com/user/031267/view?username=zhangsan&age=20
请求URL : http://www.test.com/user/031267/view

@RequestMapping(value = "/user/{userid}/view")  //占位符 userid
public String view ( @PathVariable("userid") String userid){ //@PathVariable 中指定 userid
    System.out.println("userid= "+userid);   //此处可以获取:userid= 031267
    return SUCCESS;
}

2、@RequestParam :获取request请求参数中的值

请求路径:http://www.test.com/user/query?username=zhangsan&age=20
请求参数:username=zhangsan&age=20

@RequestMapping(value = "/user/query")
public String query(@RequestParam(value="username") String username) {	
	System.out.println("username = " + username);   //此处打印:username = zhangsan
	return SUCCESS;
}

@RequestParam有三个配置参数:

required 表示是否必须,默认为 true,必须。
defaultValue 可设置请求参数的默认值。
value 为接收url的参数名(相当于key值)。
@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。

@PathVariable 与 @RequestParam 的区别:

  • 使用 @RequestParam 时,URL是这样的:http://host:port/path?参数名=参数值
  • 使用@PathVariable时,URL是这样的:http://host:port/path/参数值 (用在resultful接口的传参)

3、@RequestBody :获取 request 请求体 body 的值

  1. 仅限用于post请求
  2. 因为一次请求只能有一个请求体body,所以方法参数中也只能有一个@RequestBody参数。但是,也可以把 请求体body的对象拆分开,用多个@RequestBody参数来接收。
  3. 一般情况下,@RequestBody只处理 Content-Type 是 application/json 或者 application/xml。
	@PostMapping
    public String add(@RequestBody User user)
    {
       return "";
    }

@RequestBody 与 RequestParam区别:

  1. 如果为get请求时,后台接收参数的注解应该为RequestParam
  2. 如果为post请求时,则后台接收参数的注解就是为RequestBody
三、使用场景

创建实体类Person:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    private Integer id;
    private Integer price;
    private String name;

}

创建控制层controller:

测试一:Get请求的参数可以通过@PathVariable和@RequestParam获取

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/path")
public class PersonController {

    
    @GetMapping("/person/{id}/view")
    public String getPerson(@PathVariable(value = "id") Integer id,
                            @RequestParam(value = "name") String name,
                            @RequestParam(value = "price", required = false, defaultValue = "0") Integer price) {
        String result = "id:" + id + ",name:" + name + ",price:" + price;
        return result;
    }
}

postman测试:

测试二:Post使用@RequestBody注解将Json格式的参数自动绑定到Entity类

    @PostMapping("/person/check")
    public String checkPerson(@RequestBody Person person) {
        String result = "id:" + person.getId() + ",name:" + person.getName() + ",price:" + person.getPrice();
        return result;
    }

postman测试:

测试三:Post使用@RequestParam获取请求体中非Json格式的数据

    @PostMapping("/person/checkmore")
    public String checkMore(@RequestParam(value = "amount") Integer amount, @RequestParam(value = "discount") float discount) {
        String result = "amount:" + amount + ",discount:" + discount;
        return result;
    }

postman测试:


测试四:Post请求也可以直接与对象类绑定,但需要参数名一致,不支持json格式,只支持form-data和x-www.form-urlencoded格式

    @PostMapping("/person/add")
    public String addPerson(Person person) {
        String result = "id:" + person.getId() + ",name:" + person.getName() + ",price:" + person.getPrice();
        return result;
    }

postman测试:
注意:如果用raw测试:

可以看到,没有赋值,都是null。

用form-data和x-www.form-urlencoded格式测试:

form-data测试:

x-www.form-urlencoded测试:

测试五:Put请求可以直接与对象类绑定,但需要参数名一致

 
    @PutMapping("/person/{id}/update")
    public String updatePerson(@PathVariable(value = "id") Integer id, Person person) {
        String result = "pathid:" + id + "===Person(id:" + person.getId() + ",name:" + person.getName() + ",price:" + person.getPrice() + ")";
        return result;
    }

postman测试:

注意点:
1、上面测试返回结果字符串前面都加了convertString ==,是因为项目中添加了数据绑定器,详情请见:https://blog.csdn.net/m0_58680865/article/details/125006308

2、测试返回结果中,并不是所有的字符串前面都添加了前缀convertString ==,仔细观察会发现,只有当字符串参数作为独立个体接收参数的时候会添加前缀,当参数是bean实体的时候,即使实体中的属性是字符串,还是无法添加前缀

3、postman测试,如下图,Params和Body同时开着可能会影响测试结果,测试时只保留一个,另一个关闭。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937345.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号