1 查询参数

请求格式:url?参数1=值1&参数2=值2...
同时适用于GET和POST方式
spring处理查询参数的方法又有几种写法:

方法一:
方法参数名即为请求参数名

  // 查询参数1
@RequestMapping(value = "/test/query1", method = RequestMethod.GET)
public String testQuery1(String username, String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法二:
从HttpServletRequest中提取参数

  // 查询参数2
@RequestMapping(value = "/test/query2", method = RequestMethod.GET)
public String testQuery2(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法三:
方法参数名和请求参数名可以不一样,通过@RequestParam注解来绑定参数

  // 查询参数3
@RequestMapping(value = "/test/query3", method = RequestMethod.GET)
public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
System.out.println("username=" + un + ", password=" + pw);
return "username=" + un + ", password=" + pw;
}

方法四:
创建一个实体类对象作为参数承载体,spring会根据参数名称自动将参数绑定到实体类对象的属性上

  // 查询参数4
@RequestMapping(value = "/test/query4", method = RequestMethod.GET)
public String testQuery4(User user) {
String username = user.getUsername();
String password = user.getPassword();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

实体类定义如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
private String username;
private String password;
}

这里用到了第三方库lombok,这样就不需要在代码中手动添加get、set等方法,lombok会自动添加。

发送请求的curl命令如下:

curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'

交互报文如下:

GET /test/query1?username=aaa&password=bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */* HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:01:30 GMT username=aaa, password=bbb

2 表单参数

请求参数不在url中,而是在Body体中,格式为:url?参数1=值1&参数2=值2...
适用于POST方式
表单参数处理方法和前面的请求参数处理方法几乎完全一样,只是RequestMethod注解中将method方法设置成POST方法

方法一:

  // 表单参数1
@RequestMapping(value = "/test/form1", method = RequestMethod.POST)
public String testForm1(String username, String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法二:

  // 表单参数2
@RequestMapping(value = "/test/form2", method = RequestMethod.POST)
public String testForm2(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法三:

  // 表单参数3
@RequestMapping(value = "/test/form3", method = RequestMethod.POST)
public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
System.out.println("username=" + un + ", password=" + pw);
return "username=" + un + ", password=" + pw;
}

方法四:

  // 表单参数4
@RequestMapping(value = "/test/form4", method = RequestMethod.POST)
public String testForm4(User user) {
String username = user.getUsername();
String password = user.getPassword();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

curl请求命令如下:

curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1

请求和响应报文如下:

POST /test/form1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded username=aaa&password=bbb HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:05:35 GMT username=aaa, password=bbb

3 路径参数

请求参数为url中的一部分,格式为:url/参数1/参数2...
同时适用于GET和POST方式
代码如下:

  @RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)
public String testUrl(@PathVariable String username, @PathVariable String password) {
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

请求curl命令如下:

curl -i http://192.168.1.14:8080/test/url/aaa/bbb

请求和响应报文如下:

GET /test/url/aaa/bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */* HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:07:44 GMT username=aaa, password=bbb

4 json格式参数

请求参数在Body体中,并且为json格式。需要添加请求头:Content-Type: application/json;charset=UTF-8
适用于POST方式
方法一:
定义实体类,将json对象解析成实力类,需要添加RequestBody注解

  // json参数1
@RequestMapping(value = "/test/json1", method = RequestMethod.POST)
public String testJson1(@RequestBody User user) {
String username = user.getUsername();
String password = user.getPassword();
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法二:
如果不像定义实体类,也可以将json请求直接解析成JSONObject类

  // json参数2
@RequestMapping(value = "/test/json2", method = RequestMethod.POST)
public String testJson2(@RequestBody JSONObject json) {
String username = json.getString("username");
String password = json.getString("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

方法三:
也可以将json对象直接解析成Map对象

  // json参数3
@RequestMapping(value = "/test/json3", method = RequestMethod.POST)
public String testJson3(@RequestBody Map<String, String> userMap) {
String username = userMap.get("username");
String password = userMap.get("password");
System.out.println("username=" + username + ", password=" + password);
return "username=" + username + ", password=" + password;
}

请求curl命令如下:

curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '
{
"username" : "aaa",
"password" : "bbb"
}
' http://192.168.1.14:8080/test/json1

请求和响应报文如下:

POST /test/json1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json;charset=UTF-8
Content-Length: 52 {
"username" : "aaa",
"password" : "bbb"
}
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:09:06 GMT username=aaa, password=bbb

Spring接收web请求参数的几种方式的更多相关文章

  1. 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数

    在Web项目中,如果前端MVC框架使用的是SpringMVC,可以使用Map接收前端请求参数,比bean要方便很多. 尤其是SpringMVC和Mybatis一起用的时候,用Map大大减少了需要的be ...

  2. spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  3. Spring Controller 获取请求参数的几种方法

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...

  4. Java Spring Controller 获取请求参数的几种方法

    技术交流群:233513714  1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...

  5. springMVC接收请求参数的几种方式

    1.  用注解@RequestParam绑定请求参数 用注解@RequestParam绑定请求参数a到变量a,当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,例如: ...

  6. android向web提交参数的4种方式总结,附带网站案例源码

    第一种:基于http协议通过get方式提交参数 1.对多个参数的封装 public static String get_save(String name, String phone) { /** * ...

  7. Spring 中解析 URL参数的几种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  8. spring mvc获取路径参数的几种方式

    一.从视图向controller传递值,  controller <--- 视图 1.通过@PathVariabl注解获取路径中传递参数 (参数会被复制到路径变量) @RequestMappin ...

  9. jmeter请求参数的两种方式

    Jmeter做接口测试,Body与Parameters的选取 1.普通的post请求和上传接口,选择Parameters. 2.json和xml请求接口,选择Body. 注意: 在做接口测试时注意下请 ...

随机推荐

  1. IDC 内网机器 通 过 iptables SNAT上网的配置方法

    有三台机器, A .B .C,其中A机器有外网和内网IP,B和C只有内网,我们配置B和C二台机器通过A机器来上外网. 假设A机器 外网IP为: 60.12.13.14  内网IP为: 192.168. ...

  2. ubuntu安装IDEA和PYCHARM

    IDEA和PYCHAR的下载以及安装步骤一样. 1.下载免费学习版本(Community) 2.解压文件到opt文件夹下面sudo tar -zxvf xxx -C /opt 3.进入解压之后的bin ...

  3. Ruby模块与类

    Ruby是单一继承,一个类只能有一个直接的母类 Mix-in 模式来处理复合需求 多重包含: 由下往上回溯,重复的只看最上方的      换句话说,程序由上往下执行,由具体到抽象,第二次发现同一个模块 ...

  4. 洛谷 P3358 最长k可重区间集问题 【最大费用最大流】

    同 poj 3680 https:www.cnblogs.com/lokiii/p/8413139.html #include<iostream> #include<cstdio&g ...

  5. pyinstaller打包.py程序为.exe操作指南

    pyinstaller打包.py程序为.exe操作指南 20190526内容纲要: 1.pyinstaller安装 2.程序封装 3.可执行程序 0 前言 今天第一次试试将一个py程序封装成一个.ex ...

  6. Luogu P1186 玛丽卡 【最短路】By cellur925

    题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...

  7. Elasticsearch 2.3.2 安装部署

    先按照http://blog.csdn.net/love13135816/article/details/51690280这个教程安装, 不过后面的IK分词器安装部分有问题. 所以中文分词器插件的安装 ...

  8. jenkins构建maven项目

    使用jenkins构建部署maven项目 因为一开始我们是自定义插件,所以构建项目的时候没有显示maven风格的项目.如果要想使用maven,我们这里必须得安装一下插件,我们在插件管理器中, 可以看到 ...

  9. [CERC2017]Buffalo Barricades

    这个题目,扫描线+玄学** 大概操作就是用个扫描线从上往下扫. 博主有点懒,就直接贴代码了,但是我还是给大家贴个比较详细的博客,除了代码都可以看wym的博客,我基本上就是按wym大佬的思路来的,当然, ...

  10. 题解报告:hdu 1570 A C

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1570 Problem Description Are you excited when you see ...