注解之@RequestParam和@GetMapping
@RequestParam用来处理Content-Type 为 application/x-www-form-urlencoded编码的内容,将请求参数名映射到方法参数名。在Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型。接下来我们结合测试用例看一下@RequestParam注解的三个主要参数。
@RestController
@RequestMapping("/user")
public class UserController {
private static Logger logger = LoggerFactory.getLogger(UserController.class); @GetMapping("/viewUserByBean")
public User viewUserByBean(User user) {
logger.info("@GetMapping中请求参数 ownerId = " + user.getId());
user.setName(user.getName() + " --> lucy");
return user;
} @RequestMapping(value ="/viewUserByEachEle",method = RequestMethod.GET)
public User viewUserByEachEle(@RequestParam(value = "id", required = false, defaultValue = "1") Long ownerId,
@RequestParam("userName") String name) {
logger.info("请求参数 ownerId = " + ownerId);
User user = new User();
user.setId(ownerId);
user.setName(name + " --> lucy");
return user;
} @GetMapping("/testRequestParam")
public String testRequestParam(@RequestParam(value = "id", required = false, defaultValue = "1") Long ownerId,
@RequestParam("userName") String name) {
logger.info("testRequestParam, userName: " + name + ", id: "
+ ownerId);
return "SUCCESS";
} }
// 定义User Bean
public class User implements Serializable { private static final long serialVersionUID = 7797704227043955944L; private Long id;
private String name; // omit getter、setter and toString
}
使用postman模拟ajax请求,URL为http://localhost:8080/user/viewUserByEachEle?userName=licy&id=1002:

@RequestParam
@RequestParam可以接受简单类型的属性,也可以接受对象类型(如viewUserByBean方法)。创建一个实体类对象作为参数承载体,Spring MVC会根据参数名称自动将参数绑定到实体类对象的属性上,viewUserByBean就是这么处理的。
value:String 类型,请求参数名,如viewUserByEachEle 方法中的userName表示请求参数名,它的值将被绑定到方法参数name。
required:是否必须,默认值为true,表示请求参数中必须包含对应的参数,否则,将抛出400错误码;异常信息是org.springframework.web.bind.MissingServletRequestParameterException,提示“Required String parameter 'userName' is not present”。
defaultValue:String 类型,表示如果请求中没有同名参数时的默认值,默认值可以是SpEL表达式,如“#{systemProperties['java.vm.version']}”。
其实如果不使用@RequestParam,Spring MVC也会将request的parameter自动绑定到method的parameter中,使用@RequestParam只不过是对parameter进行配置和对URL更精确化的配置。例如,在请求参数名和方法参数名相同时,则可以省略@RequestParam注解。
@GetMapping
@GetMapping是一个组合注解,等价于@RequestMapping(method = RequestMethod.GET),它将HTTP Get请求映射到特定的处理方法上。例如,在测试用例中,使用@GetMapping("/testRequestParam")代替了@RequestMapping(value ="/viewUserByEachEle",method = RequestMethod.GET),显然,这可以精简我们的代码。
注解之@RequestParam和@GetMapping的更多相关文章
- SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍
SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍 本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@Requ ...
- SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析
SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...
- 阶段3 3.SpringMVC·_03.SpringMVC常用注解_1 RequestParam注解
新建param.jsp页面.里面一个a标签. 新建Controller 输出字符串 重新部署 传参数 接收这个username并输出 把username换成name 接收不到. 使用RequestPa ...
- @RequestParam注解
SpringMVC的参数指定注解:@RequestParam,有下面四个方法: value 参数绑定,value里写的是URL里参数名称 name 同上 required 是否必需参数,默认为tr ...
- @RequestParam,@RequestBody,@ResponseBody,@PathVariable注解的一点小总结
一.前提知识: http协议规定一次请求对应一次响应,根据不同的请求方式,请求的内容会有所不同: 发送GET请求是没有请求体的,参数会直接拼接保留到url后一并发送: 而POST请求是带有请求体的,带 ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
随机推荐
- 解决Cannot change version of project facet Dynamic web module to 2.5(转)
我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...
- rabbitMQ 安装,基于Windows环境
参考文章:https://www.cnblogs.com/ericli-ericli/p/5902270.htmlRabbit MQ 是建立在Erlang OTP平台上,安装前需先安装Erlang.h ...
- springboot启动流程(一)构造SpringApplication实例对象
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 启动入口 本文是springboot启动流程的第一篇,涉及的内容是SpringApplicat ...
- 初学VUE 走马灯效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- background 背景图片 --css3
background 1.设置背景平铺background-repeat round :图片会进行缩放后平铺space : 图片会进行平铺,中间留下空白空间 注:当滚动行为设为fixed,round和 ...
- vue cli3 打包到tomcat上报错问题
首先 项目打包步骤 1.vue config.js 添加 publicPath: './', // 公共路径 assetsDir:'static', 2.将代理注释掉 proxy 3.将hash需 ...
- 常用的virsh管理命令
常用的virsh管理命令 列出所有的虚拟机 [root@ubuntu ~]# virsh list --all 显示虚拟机信息 [root@ubuntu ~]# virsh dominfo CentO ...
- LAMP源码编译安装
php加速器 XCache 快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境. 项目地址:http://xcache.lighttpd.net/,收录EPEL源 实现XCach ...
- python链接sql server 乱码问题
import pymssql import sys import os reload(sys) sys.setdefaultencoding('utf-8') os.environ['NLS_LANG ...
- jQuery属性遍历、HTML操作
jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. .add() 将元素添加到匹配元素的集合中. . ...