0

0

java详解Spring接收web请求参数的方式

青灯夜游

青灯夜游

发布时间:2018-10-25 16:39:04

|

2572人浏览过

|

来源于博客园

转载

本篇文章给大家带来的内容是java详解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中提取参数

立即学习Java免费学习笔记(深入)”;

  // 查询参数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
@Builderpublic 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;
  }

方法二:

MiroThinker
MiroThinker

MiroMind团队推出的研究型开源智能体,专为深度研究与复杂工具使用场景设计

下载
  // 表单参数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=bbbHTTP/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

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

2

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

2

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

0

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

0

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

25

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

622

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 3万人学习

C# 教程
C# 教程

共94课时 | 7.9万人学习

Java 教程
Java 教程

共578课时 | 53.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号