SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)
上一篇SpringBoot实战(二)Restful风格API接口中写了一个控制器,获取了前端请求的参数,现在我们就参数的获取与校验做一个介绍:
一:获取参数
SpringBoot提供的获取参数注解包括:@PathVariable,@RequestParam,@RequestBody,三者的区别如下表:

示例代码:
Order:
package com.example.demo.controller.user.entity;
public class Order {
private Integer id;
private String name;
private Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
OrderController
package com.example.demo.controller.user.controller; import com.example.demo.controller.user.entity.Order;
import org.springframework.web.bind.annotation.*; @RestController
public class OrderController { /**
* Get请求的参数可以通过@PathVariable和@RequestParam获取
* @param id 必填
* @param name 必填
* @param price 选填,默认值为0
* @return
*/
@GetMapping("/orders/{id}")
public String getOrder(@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;
} /**
* Post使用@RequestBody注解将Json格式的参数自动绑定到Entity类
* @param order
* @return
*/
@PostMapping("/order/check")
public String checkOrder(@RequestBody Order order){
String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
return result;
} /**
* Post使用@RequestParam获取请求体中非Json格式的数据
* @param amount
* @param discount
* @return
*/
@PostMapping("/order/checkmore")
public String checkMore(@RequestParam(value = "amount")Integer amount, @RequestParam(value = "discount")float discount){
String result = "amount:"+amount+",discount:"+discount;
return result;
} /**
* Post请求也可以直接与对象类绑定,但需要参数名一致,不支持json格式,只支持form-data和x-www.form-urlencoded格式
* @param order
* @return
*/
@PostMapping("/order/add")
public String addOrder(Order order){
String result = "id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice();
return result;
} /**
* Put请求可以直接与对象类绑定,但需要参数名一致
* @param id
* @param order
* @return
*/
@PutMapping("/order/{id}/update")
public String updateOrder(@PathVariable(value = "id")Integer id,Order order){
String result = "pathid:"+id+"===Order(id:"+order.getId()+",name:"+order.getName()+",price:"+order.getPrice()+")";
return result;
} }
注意点:
1.针对一些非必填的参数,可以使用required关键字来标识,同时必须设置默认值defaultValue,如getOrder方法中对price参数的获取:
@RequestParam(value = "price",required = false,defaultValue = "0") Integer price
2.参数可以直接与Entity类绑定,但不支持json格式,只支持form-data和x-www.form-urlencoded格式
@PostMapping("/order/add")
public String addOrder(Order order){
3.使用的Postman做的测试,所有接口都测试通过,也推荐大家使用Postman作为日常的接口测试工具,安装和操作都很简单。
附部分截图:
Get:@PathVariable,@RequestParam

Post:@RequestBody

获取到参数以后就是要对数据做校验了,在下一篇中进行介绍
SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)的更多相关文章
- SpringBoot 拦截器获取http请求参数
SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...
- SpringMVC参数绑定(从请求中接受参数)
参数绑定(从请求中接收参数) 1)默认类型: 在controller方法中可以有也可以没有,看自己需求随意添加. httpservletRqeust,httpServletResponse,httpS ...
- soapUI使用-DataSource获取oracle库中的参数
soapUI使用-DataSource获取oracle库中的参数 下载mysql和oracle驱动包:http://pan.baidu.com/s/1i3sy1MH 放在Program Files\S ...
- 获取url路径中的参数
简介 运用js的时候,我们有时可能会有这样的需求,就是想要获取浏览器地址栏指定的一项参数,形如:https://i.cnblogs.com/EditPosts.aspx?postid=8628413& ...
- 获取网页url中的参数
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用JavaScript获取url(request)中的参数
这次是使用JavaScript来获取url(request)中的参数 在日常页面编写的过程中为了方便操作在<script>中通过使用window.location.href="要 ...
- SpringMVC无法获取请求中的参数的问题的调查与解决(1)
*更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...
- Spring MVC如何获取请求中的参数
目录 一.获取URL中路径参数 1.1 @PathVariable 注解 1.2 @PathParam 注解 二.获取请求参数: 2.1 GET请求 2.1.1 获取请求中的单个参数:@Request ...
- spring boot拦截器中获取request post请求中的参数
最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...
随机推荐
- 在HTTP页面输入数据,Chrome 70将显示红色不安全警告
2018年10月17日,Chrome 70版本正式发布,该版本更新多项安全功能,再次升级对HTTP页面的不安全警告样式.当用户在HTTP页面输入数据时,Chrome 70将显示醒目的红色不安全警告,让 ...
- 51nod-字符串连接
输入n个字符串s[i],你要把他们按某个顺序连接起来,使得字典序最小. (1 <= n <= 100) (每个字符串长度 <= 100) (字符串只包含小写字母) Input 第一行 ...
- 一、frp官方中文文档
frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp, http, https 协议. 目录 frp 的作用 开发状态 架构 使用示例 通过 ssh 访问公司内网机器 通过自定义 ...
- 启用Maven的代理访问
1. Maven配置文件 找到文件 {M2_HOME}/conf/settings.xml, 并把你的代理服务器信息配置写入.注:{M2_HOME} => D:\software\yiibai. ...
- ARM - Linux嵌入式C/C++各种资料分享【更新日期:2012/04/24】
http://blog.csdn.net/shuxiao9058/article/details/6786868 由于115网盘取消大众分享功能,因此不能继续分享下载链接.更新资料将在本人分享空间转存 ...
- 2019-03-20 用SSIS把Excel中的数据导出来保存到SQLServer中
Control Flow 1.配置 好 图形 2.去变量那 配置好 文件路径 和 存储过程 3.在SQL Server创建对应的存储过程,该存储过程的功能是每次导入是清空原有的数据 4.如果不懂的参考 ...
- myeclipse反编译安装 jd-gui.exe下载
一:在线安装 1.Help->Install New Site Name:** Location:http://jd.benow.ca/jd-eclipse/update 二:手动安装 1.下载 ...
- Spring MVC中 提交表单报错400
背景: 在写SpringMVC表单提交的代码的时,在最后点击提交的时候总是会出现400的错误 原因: 主要原因就是表单提交的数据和对应实体类的属性无法完全匹配 解决方案: 查看我们提交的数据是否完全和 ...
- SpringMVC请求@RequestParam中文乱码解决
private String encodeStr(String str) { try { return new String(str.getBytes("ISO-8859-1"), ...
- 小贝_mysql三种子查询
mysql三种子查询 简要: 一.三种子查询 二.三种子查询理解模型 一.mysql 三种子查询 where子查询.from子查询.exists子查询 二.理解模型: 2.1.一个好的模型,便于我们去 ...