@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的更多相关文章

  1. SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍 本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@Requ ...

  2. SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析

    SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...

  3. 阶段3 3.SpringMVC·_03.SpringMVC常用注解_1 RequestParam注解

    新建param.jsp页面.里面一个a标签. 新建Controller 输出字符串 重新部署 传参数 接收这个username并输出 把username换成name 接收不到. 使用RequestPa ...

  4. @RequestParam注解

    SpringMVC的参数指定注解:@RequestParam,有下面四个方法:   value 参数绑定,value里写的是URL里参数名称 name 同上 required 是否必需参数,默认为tr ...

  5. @RequestParam,@RequestBody,@ResponseBody,@PathVariable注解的一点小总结

    一.前提知识: http协议规定一次请求对应一次响应,根据不同的请求方式,请求的内容会有所不同: 发送GET请求是没有请求体的,参数会直接拼接保留到url后一并发送: 而POST请求是带有请求体的,带 ...

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

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

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

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

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

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

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

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

随机推荐

  1. winform c# 请求网站,返回Json字符串

    private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...

  2. 2019 WebRtc AudioMixer混音流程

    本文简要说明最新版WebRtc AudioMixer混音流程. 本程序使用4个16KHz 单声道时长均大于10秒的Wav文件作为混音源,只合成前10秒的音频,输出也是16KHz单声道音频. 输入和输出 ...

  3. JS实现当前选择日期是星期几

    使用到的日期插件是My97 Datepicker,这里通过onpicked方法触发getDay()方法,在getDay()方法中获取已选择的日期来判断是星期几. 插件下载地址:http://www.m ...

  4. MYSQL日期相关操作

    *******MYSQL中取当前周/月/季/年的第一天与最后一天******* 当年第一天: SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 ...

  5. Java基础加强-代理

    /*代理*//*代理的概念与作用*/ 代理过程架构 客户端Client原来直接调用的是Target目标类 使用代理后,现在让客户端不要调用Target,调用代理类Proxy,代理类Proxy和目标类T ...

  6. CentOS linux7 设置开机启动服务

    常用命令 描述                                 旧命令  新命令 使服务自动启动          chkconfig --level 3 http on  syste ...

  7. inotify和rsync实现数据实时同步

    数据的实时同步 实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 实现实时同步的方法 ino ...

  8. 关于在window8上使用ssh命令的记录

    1.开启虚拟机以及git bash窗口,准备连接 2.在虚拟机中输入ifconfig -a查看虚拟机ip 从图中找到ip为 : inet 地址:192.168.78.133 3.输入命令: ssh r ...

  9. 【2017-09-04】JavaWeb内置对象

    Jsp页面中引入别的页面 include命令 <%@ include file="要引入的页面路径"%> 页面中的form表单提交方式: post: 提交内容不可见, ...

  10. Const *ptr ptr

    1. const int *ptr = NULL; <=> int const *ptr = NULL; 1) 表示指向符号常量的指针变量,指针变量本身并非const所以可以指向其他变量. ...