SpringMVC 参数传递和接收的几种方式
普通传参
测试项目:SpringBoot2.0。不使用 form 表单传参,后端不需要指定 consumes 。
使用 Postman 进行测试。
@PathVariable
只能接收 URL 路径里的参数。
@RequestParam
只能接收 URL 问号后跟着的参数,不管是 GET 还是 POST,虽然一般只有 GET 请求才会在 URL 后边跟参数,问号 ? 后面的部分,使用 & 区分参数。
http://localhost:8080/api/user/login/test?username=2222222&pass=333333333
@RequestParam("username")String username,
@RequestParam("pass")String pass
@RequestBody
只能接收请求体中的参数,也就是只能是 POST 请求才有请求体,GET 请求没有请求体,请求体分两种情况参数
(1)使用String接收
比如前端参数在请求体中传的是 username=18514335982&pass=12345,Content type 为 text/plain;charset=UTF-8
则后台接收到的 param 即为 username=18514335982&pass=12345 格式
@RequestBody String param
(2)使用封装的 bean 或者 JSONObject 接收(常用)
前端必须使用 JSON 格式的数据,Content-Type 必须为 application/json,请求体中参数为 {"username":"18514335982","pass":"12345"}
@RequestBody User user
@RequestBody JSONObject jsonObject
测试代码
@PostMapping("/login/test")
public ResultBuilder userLogin1(@RequestHeader(Constants.ACCEPT_VERSION)String version,
@RequestHeader(Constants.ACCESS_TOKEN)String token,
@RequestParam("username")String username,
@RequestParam("pass")String pass,
@RequestBody User user){
logger.debug("username======" + username);
logger.debug("pass======" + pass);
logger.debug("user---username==" + user.getUsername());
logger.debug("user---pass==" + user.getPass());
return new ResultBuilder(StatusCode.SUCCESS);
}
FORM表单传参
测试项目:SpringBoot2.0
GET方式
前端表单传参
<form action="http://localhost:8080/test" method="get">
<input type="text" name="username" />
<input type="text" name="password"/>
<input type="submit" value="Submit" />
</form>
后端参数接收,因为 form 表单使用 get 方法的时候,Content type 的值默认为空。所以后台接收代码不需要指定 consumes 属性
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test1(@RequestParam(value="username") String username,
@RequestParam(value="password") String password){
System.out.println("======GET======");
System.out.println("username=" + username);
System.out.println("password=" + password);
return "SUCCESS";
}
打印结果:
======GET======
username=wangbo
password=123456
可以看出 FORM 表单发出的 GET 请求直接通过注解 @RequestParam 进行参数接口即可。
POST方式
前端表单传参
<form action="http://localhost:8080/test" method="post">
<input type="text" name="username" />
<input type="text" name="password"/>
<input type="submit" value="Submit" />
</form>
后端参数接收,
因为 form 表单使用 post 方法的时候,Content type 的值默认为 application/x-www-form-urlencoded;charset=UTF-8。所以后台接收代码需要指定 consumes 属性。
consumes = "application/x-www-form-urlencoded;charset=UTF-8"
@ResponseBody
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded;charset=UTF-8")
public String test(@RequestParam(value="username") String username,
@RequestParam(value="password") String password,
User user){
System.out.println("======POST======");
System.out.println("username=" + username);
System.out.println("password=" + password);
System.out.println("user=" + user);
return "SUCCESS";
}
打印结果
======POST======
username=wangbo
password=123456
user=username=wangbo; password=123456
可以看出,FORM 表单发出的 POST 请求可以直接通过注解 @RequestParam 进行参数接收,
也可以使用字段对应封装的 Java Bean 对象来接收参数。注意 Java Bean 对象上不需要注解。
User 代码,为了更清楚的打印对象,重写了 toString 代码。
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "username=" + username +"; password=" + password;
}
}
SpringMVC 参数传递和接收的几种方式的更多相关文章
- springmvc url处理映射的三种方式:
一.SpringMVC简介 SpringMVC是一种基于Spring实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,使用了MVC架构模式的思想,将web层进行职责解耦,并管理应用所需对象 ...
- C#中POST数据和接收的几种方式(抛砖引玉)
POST方式提交数据,一种众所周知的方式: html页面中使用form表单提交,接收方式,使用Request.Form[""]或Request.QueryString[" ...
- [转]C#中POST数据和接收的几种方式
POST方式提交数据,一种众所周知的方式: html页面中使用form表单提交,接收方式,使用Request.Form[""]或Request.QueryString[" ...
- C#中POST数据和接收的几种方式
POST方式提交数据,一种众所周知的方式: html页面中使用form表单提交,接收方式,使用Request.Form[""]或Request.QueryString[" ...
- 【Spring】SpringMVC非注解配置的两种方式
目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...
- springmvc+jpa实现分页的两种方式
1.工具类 public final class QueryTool { public static PageRequest buildPageRequest(int pageNumber, int ...
- SpringMVC返回json数据的三种方式
1.第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew. 如:<bean id="defaultJsonView" cla ...
- SpringMVC返回json数据的三种方式(转)
原文:https://blog.csdn.net/shan9liang/article/details/42181345# 1.第一种方式是spring2时代的产物,也就是每个json视图contro ...
- springmvc获取资源文件的两种方式(超简单)
1 比如我们在sc目录下新建一个db.properties文件内容如下 DriverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306 ...
随机推荐
- (二)Web应用体系结构
容器 Servlet没有main()方法,它们受控于另一个Java应用,这个Java应用称为容器(Container).我们最常见的tomcat就是这样一个容器. Web服务器应用(如Apache)得 ...
- txn.go
package clientv3 import ( "sync" pb "github.com/coreos/etcd/etcdserver/etcdse ...
- BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针
BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针 Description Farmer John's N cows (1 <= N <= 100,000) ...
- Python Django 2.1登录功能_1
#在上篇的基础上进行#在.../sign/templates/index.html文件,开发登录表单 <html> <head> <title>Django Pag ...
- Mysql8.0命令
1.创建用户 create user 'username'@'localhost' identified by 'pwd' 2.修改访问权限 在mysql数据下修改user表用户host为'%' up ...
- Postman-----Response body:JSON value check的使用介绍
Response body:JSON value check :检查Response返回的body的某个字段所对应的值是否与预期结果的值相等 1.直接点击 Response body:JSON ...
- The following untracked working tree files would be overwritten by merge
git pull的时候遇到这样的问题: The following untracked working tree files would be overwritten by merge balabal ...
- Django_cookie_session
登录时候后台打印request.COOKIE 1.login页面正确登录的话,后台页面可以获取到浏览器携带的cookie的. 2.第一行的sessionid其实就是cookie值 3.session的 ...
- ASP.NET Core2.1 你不得不了解的GDPR(Cookie处理)
前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及待的的下载了最新的版本,然后生成了一个模版项目来试试水. ...然后就碰到问题了... 我发现..cookie竟然存不进去了.. ...
- netty 之 telnet HelloWorld 详解
前言 Netty是 一个异步事件驱动的网络应用程序框架, 用于快速开发可维护的高性能协议服务器和客户端. etty是一个NIO客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序.它极 ...