当前位置: 首页 > news >正文

SpringMVC中的接口传参接参总结

目录

  • 1. 注解 `@PathVariable`
    • 1.1. 方式一
    • 1.2. 方式二
      • 1.2.1. 单个参数
      • 1.2.2. 多个参数
    • 1.3. 方式三
  • 2. 注解 `@RequestParam`
    • 2.1. 方式一
    • 2.2. 方式二
    • 2.3. 方式三
      • 2.3.1. 单个参数
      • 2.3.2. 多个参数
    • 2.4. 上传文件
  • 3. 注解 `@RequestBody`
    • 3.1. 实体类
    • 3.2. 传参与接参
  • 4. 实体类传参接参
    • 4.1. 实体类
    • 4.2. 传参与接参
  • 5. 使用 `JsonObject` 接参
    • 5.1. 依赖包
    • 5.2. 传参与接参
  • 6. 使用 `String` 接参数
  • 7. 使用 `HttpServletRequest`
    • 7.1. 方式一
    • 7.2. 方式二

1. 注解 @PathVariable

1.1. 方式一

不推荐使用,仅在单个参数的时候选择使用

@GetMapping("/getId/{id}")
@ResponseBody
public String pathVariableTest(@PathVariable Integer id) {
	return "id:   " + id;
}

1.2. 方式二

推荐使用,适用于单个、多个参数的情况

1.2.1. 单个参数

@GetMapping("/getId/{id}")
@ResponseBody
public String pathVariableTest(@PathVariable("id") Integer id) {
	return "id:   " + id;
}

1.2.2. 多个参数

@GetMapping("/getIdAny/{id}/{name}")
@ResponseBody
public String pathVariableTestAny(@PathVariable("id") Integer id, @PathVariable("name") String name) {
	return "id:" + id + "name:" + name;
}

1.3. 方式三

@GetMapping("/getId/{idValue}")
@ResponseBody
public String pathVariableTest(@PathVariable("idValue") Integer id) {
	return "id:   " + id;
}

以上三种方式的调用结果都是成功的

在这里插入图片描述

2. 注解 @RequestParam

@RequestParam:将请求参数绑定到你控制器的方法参数上,是 SpringMVC 中接收普通参数的注解

@RequestParam(value = "参数名", required = "true", defaultValue= " ")
  • valuename :参数名
  • required:是否包含该参数,默认为 true,表示该请求路径中必须包含该参数,如果不包含就报错
  • defaultValue:默认参数值,如果设置了该值,required = true 将失效,自动为 false,如果没有传该参数,就使用默认值

2.1. 方式一

不推荐使用,仅在单个参数的时候选择使用

@GetMapping("/getId")
@ResponseBody
public String requestParamTest(Integer id) {
	return "id:" + id;
}

2.2. 方式二

不推荐使用,仅在单个参数的时候选择使用

@GetMapping("/getId")
@ResponseBody
public String requestParamTest(@RequestParam Integer id) {
	return "id:" + id;
}

2.3. 方式三

推荐使用,适用于单个、多个参数的情况

2.3.1. 单个参数

@GetMapping("/getId")
@ResponseBody
public String requestParamTest(@RequestParam("idValue") Integer id) {
	return "id:" + id;
}

可以看到在 @RequestParam 里面给参数 id 取名为: idValue,这时候传参时参数名称也需要写成 idValue 即可

2.3.2. 多个参数

@GetMapping("/getIdAny")
@ResponseBody
public String requestParamTestAny(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam Integer age) {
	return  "id:" + id +"name:" + name + "age:" + age;
}

在这里插入图片描述

2.4. 上传文件

使用 @RequestParam 上传文件的方式

@ResponseBody
@PostMapping("upload")
public String upload(@RequestParam("myFile") MultipartFile file)  {
	return "---file name:" + file.getOriginalFilename()+
                "---file type:" + file.getContentType()+
                "---file size:" + file.getSize();
}

在这里插入图片描述

3. 注解 @RequestBody

对应 @RequestBody 的使用,方式其实是比较多的,可以耐心看看,传参以 json 格式传递,接收方式较多(一定要记住 Content-Typeapplication/json

3.1. 实体类

public class UserOne {
    private Integer id;

    private String username;

    private Integer age;
}

Get 方式 和 Post 方式其实都是可以使用 @requestBody 的,只不过一般来说 Post 用的较多

3.2. 传参与接参

@GetMapping("/getRequestBodyValue")
@ResponseBody
public String RequestBodyTest(@RequestBody UserOne userOne) {
	return  userOne.toString();
}

在这里插入图片描述

4. 实体类传参接参

4.1. 实体类

public class UserOne {
    private Integer id;

    private String username;

    private Integer age;
}

4.2. 传参与接参

@GetMapping("/getValue")
@ResponseBody
public String entityValueTest(UserOne userOne) {
	return userOne.toString();
}

在这里插入图片描述

5. 使用 JsonObject 接参

5.1. 依赖包

<!--添加fastjson依赖-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.7</version>
</dependency>

5.2. 传参与接参

@PostMapping("/getRequestBodyValue")
@ResponseBody
public String RequestBodyTest(@RequestBody JSONObject jsonObject) {
 
	Integer id = jsonObject.getInteger("id");
    String name = jsonObject.getString("name");
    Integer age = jsonObject.getInteger("age");
 
	return  "---id:"+id+"  ---name:"+name+"  ---age:"+age;
}

在这里插入图片描述

6. 使用 String 接参数

这种情况是先使用 Stringbody 里面的数据获取出来,再转换成 JsonObject 再进行参数解析

@PostMapping("/getRequestBodyValue")
@ResponseBody
public String RequestBodyTest(@RequestBody String jsonStr) {

	// 将 jsonStr 转化为 JSONObject,再进行取值
	JSONObject jsonObject= JSON.parseObject(jsonStr); 
	Integer id = jsonObject.getInteger("id");
    String name = jsonObject.getString("username");
    Integer age = jsonObject.getInteger("age");
    return  "---id:"+id+"  ---name:"+name+"  ---age:"+age;
}

在这里插入图片描述

7. 使用 HttpServletRequest

7.1. 方式一

@GetMapping("/getHttpServletRequest")
@ResponseBody
public String httpServletRequestValueTest(HttpServletRequest request) {

	String id = request.getParameter("id");
    String name = request.getParameter("username");
    String age = request.getParameter("age");
    return  "---id:"+id+"  ---name:"+name+"  ---age:"+age;
}

7.2. 方式二

@GetMapping("/getHttpServletRequest")
@ResponseBody
public String httpServletRequestValueTest(HttpServletRequest request) {
 
	Map<String, String[]> parameterMap = request.getParameterMap();
    String[] ids = parameterMap.get("id");
    String[] names = parameterMap.get("username");
    String[] ages = parameterMap.get("age");
    String id = ids[0];
    String name =names[0];
    String age =ages[0];
 
	return  "---id:"+id+"  ---name:"+name+"  ---age:"+age;
}

以上两种方式调用结果

在这里插入图片描述

相关文章:

  • python毕业设计项目源码选题(17)校园二手书籍交易系统毕业设计毕设作品开题报告开题答辩PPT
  • 首版次高端软件的申报材料?
  • 关于防抖和节流在前端开发中的应用
  • 姓芦男孩名字简单大气
  • vue实战-分页器
  • RNA 27 SCI文章中转录因子结合motif富集到调控网络 (RcisTarget)
  • 什么牌子的蓝牙耳机耐用又便宜?好用的蓝牙耳机品牌推荐
  • 【NeurIPS知识图谱】联邦环境下,基于元学习的图谱知识外推(阿里浙大含源码)
  • 微服务网关选型
  • python代码学习——递归函数
  • 虹科方案 | 一种通过OPC技术提取数据库数据的解决方案
  • 关于自动化测试工具selenium
  • 某IOT设备漏洞分析
  • 毕设必备!Python智慧教室:考试作弊系统、动态点名等功能
  • 【Go】【反射】反射基本介绍和使用
  • 【node学习】协程
  • 【刷算法】求1+2+3+...+n
  • Angular Elements 及其运作原理
  • HTTP--网络协议分层,http历史(二)
  • javascript从右向左截取指定位数字符的3种方法
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • linux学习笔记
  • Mocha测试初探
  • MySQL的数据类型
  • PaddlePaddle-GitHub的正确打开姿势
  • React Transition Group -- Transition 组件
  • vuex 笔记整理
  • 来,膜拜下android roadmap,强大的执行力
  • 少走弯路,给Java 1~5 年程序员的建议
  • 网页视频流m3u8/ts视频下载
  • 我与Jetbrains的这些年
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • (1)SpringCloud 整合Python
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (二)springcloud实战之config配置中心
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (转)Mysql的优化设置
  • (转)我也是一只IT小小鸟
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .Net各种迷惑命名解释
  • .net和php怎么连接,php和apache之间如何连接
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [AutoSAR系列] 1.3 AutoSar 架构
  • [BUG] Authentication Error
  • [BZOJ4010]菜肴制作
  • [C++提高编程](三):STL初识
  • [CF407E]k-d-sequence
  • [HJ73 计算日期到天数转换]
  • [JavaWeb学习] tomcat简介、安装及项目部署
  • [loj6039]「雅礼集训 2017 Day5」珠宝 dp+决策单调性+分治
  • [Oh My C++ Diary]用cout输出时后endl的使用
  • [one_demo_8]十进制转二进制
  • [PHP] 面向对象