热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Spring接收web请求参数的几种方式

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

Spring接收web请求参数的几种方式

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 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

 

posted on 2018-10-25 15:02 lasdaybg 阅读(...) 评论(...) 编辑 收藏


推荐阅读
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • PHP中的curl_multi系列函数可以实现同时请求多个URL来实现并发,而不是像普通curl函数那样请求后会阻塞,直到结果返回才进行下一个请求。因此在批量请求URL时可通过curl_multi系列函数提升程序的运行效率。curl普通请求$startT ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
author-avatar
KING树林_944
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有