一、前言

@RequestParam、@RequestBody、@PathVariable都是用于在Controller层接收前端传递的数据,他们之间的使用场景不太一样,今天来介绍一下!!

二、实体类准备

@Data
public class Test implements Serializable { private String id; private String name; private String state; private String createTime; }

三、@RequestParam

  • 定义

一个请求,可以有多个RequestParam

@RequestParam 接收普通参数的注解 一般与get请求一起使用

@RequestParam(value="参数名",required="true/false",defaultValue="如果没有本值为这个参数的值")

required默认为true,当为false是,才可以使用defaultValue

  • 案例
	@GetMapping("/getDataById")
public String getDataById(@RequestParam(value = "id",required = false,defaultValue = "1") String id){ //使用mybatis-plus来根据id查询数据
Test test = testMapper.selectById(id); return test.toString(); //结果: Test{id='1', name='dd', state='A', createTime='null'}
}

四、@RequestBody

  • 定义

一个请求,只有一个RequestBody

@RequestBody(required="true/false")

@RequestBody:一般来接受请求体中json的注解 一般与post请求一起使用

required默认为true(必传,要不报错)

  • 案例
@PostMapping("/insertData")
public int insertData(@RequestBody Test test){ //使用mybatis-plus来插入新数据
int insert = testMapper.insert(test); return insert; //结果: 1
}

五、@PathVariable

  • 定义

一个请求,可以有多个PathVariable

@PathVariable 映射URL绑定的占位符 一般与get请求一起使用

@PathVariable(value="参数名",required="true/false")

  • 案例
	@GetMapping("/getById/{id}")
public String getById(@PathVariable String id){
//使用mybatis-plus来根据id查询数据
Test test = testMapper.selectById(id); return test.toString(); //结果: Test{id='1', name='dd', state='A', createTime='null'}
}

六、区别和使用场景

@RequestParam一般在get请求时,参数是一个个的参数时,请求url一般为http://localhost:8089/test/getDataById?id=1

@RequestBody一般在post请求时,参数是一个对象或者集合时,请求一般为json类型的请求体

@PathVariable一般在get请求时,参数是一个个的参数时,更能体现RestFul风格,请求url一般为:http://localhost:8089/test/getDataById/1

@RequestParam、@RequestBody、@PathVariable区别和案例分析的更多相关文章

  1. @PathVariable @RequestParam @RequestBody 的区别

    转载自:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  2. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  3. 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...

  4. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  5. (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  6. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  7. 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox   因为写的很好很全,所以转载过来 引言:接上一篇文章, ...

  8. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  9. @RequestBody、@RequestParam、@PathVariable区别与使用场景

    由于项目是前后端分离,因此后台使用的是spring boot,做成微服务,只暴露接口.接口设计风格为restful的风格,在get请求下,后台接收参数的注解为RequestBody时会报错:在post ...

随机推荐

  1. WPF上传图片到服务器文件夹

    1.前端用ListBox加载显示多张图片 1 <ListBox Name="lbHeadImages" Grid.Row="1" ScrollViewer ...

  2. XCTF-ics-05(文件包含+preg_replace函数/e修正符下的代码执行漏洞)

    记一道preg_replace函数/e模式下的代码执行漏洞利用的题. 只有设备维护中心页面可以进入,页面没有什么可点击的,查看源代码,发现这里有个参数. 拼接到url,页面显示index,拼接/etc ...

  3. SpringBoot报错:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

    Spring Boot报错:Error starting ApplicationContext. To display the conditions report re-run your applic ...

  4. Notepad++的NppFTP插件连接linux操作系统

    Notepad++的NppFTP插件连接linux操作系统 下载地址:https://notepad-plus-plus.org/downloads/v8.1.2/ 1.安装Npp_FTP插件 两种方 ...

  5. Speed up Downloading Files on Linux

    Compared aria2c, axel and wget, aria2c is the best. It support multi-thread download (with "-s ...

  6. Java工具包之-Guava

    https://blog.csdn.net/zmx729618/article/details/78540026 https://my.oschina.net/realfighter/blog/349 ...

  7. MongoDB-04-备份和恢复

    mongodb备份和恢复 常用的备份恢复工具 1 ** mongoexport/mongoimport 2 ***** mongodump/mongorestore 备份工具区别在哪里 应用场景总结: ...

  8. Java文件和Java包结构

    Java中的包概念 Java中的包是封装一组类,子包和接口的机制.软件包用于: 防止命名冲突.例如,可以有两个名称分别为Employee的类,college.staff.cse.Employee和co ...

  9. SSRF详解

    上一篇说了XSS的防御与绕过的思路,这次来谈一下SSRF的防御,绕过,利用及危害 0x01 前置知识梳理 前置知识涉及理解此漏洞的方方面面,所以这部分要说的内容比较多 SSRF(Server-Side ...

  10. K8s 部署 Gitlab CI Runner

    K8s 版本:1.20.6 GitLab CI 最大的作用是管理各个项目的构建状态.因此,运行构建任务这种浪费资源的事情交给一个独立的 Gitlab Runner 来做就会好很多,而且 Gitlab ...