SpringBoot2:请求处理原理分析-接口参数的常用注解

本文主要是介绍SpringBoot2:请求处理原理分析-接口参数的常用注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、@PathVariable

作用说明:获取路径参数

案例:
接口收参形式:

    @GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@PathVariable("id") Integer id,@PathVariable("username") String name,@PathVariable Map<String,String> pv){Map<String,Object> map = new HashMap<>();map.put("id",id);map.put("name",name);map.put("pv",pv);return map;}

请求传参形式:

http://127.0.0.1:8080/car/3/owner/lisi

返回结果:

{"pv": {"id": "3","username": "lisi"},"name": "lisi","id": 3
}

可以用Map<String,String>接收全部路径参数。

2、@RequestHeader

作用说明:获取请求头参数

案例:
接口收参形式:

    @GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@RequestHeader("User-Agent") String userAgent,@RequestHeader Map<String,String> header){Map<String,Object> map = new HashMap<>();map.put("userAgent",userAgent);map.put("headers",header);return map;}

请求传参形式:

http://127.0.0.1:8080/car/3/owner/lisi

返回结果:

{"headers": {"user-agent": "PostmanRuntime/7.26.8","accept": "*/*","cache-control": "no-cache","postman-token": "7651f635-068b-4fe6-8e26-4990ef714349","host": "127.0.0.1:8080","accept-encoding": "gzip, deflate, br","connection": "keep-alive"},"userAgent": "PostmanRuntime/7.26.8"
}

可以用Map<String,String>接收全部Header参数。

3、@RequestParam

作用说明:获取请求参数,就是以往的GET请求传参方式。

案例:
接口收参形式:

    @GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@RequestParam("age") Integer age,@RequestParam("inters") List<String> inters,@RequestParam Map<String,String> params){Map<String,Object> map = new HashMap<>();map.put("age",age);map.put("inters",inters);map.put("params",params);return map;}

请求传参形式:

http://127.0.0.1:8080/car/3/owner/lisi?age=18&inters=basketball&inters=game

返回结果:

{"inters": ["basketball","game"],"params": {"age": "18","inters": "basketball"},"age": 18
}

可以用Map<String,String>接收全部GET请求参数参数。

4、@RequestBody

作用说明:获取form表单请求体参数,就是以往的POST请求传参方式。

案例:
接口收参形式:

    @PostMapping("/save")public Map postMethod(@RequestBody String content){Map<String,Object> map = new HashMap<>();map.put("content",content);return map;}

请求传参形式:

<form action="/save" method="post">用户名:<input name="userName"/> <br>邮箱:<input name="email"/><input type="submit" value="提交"/>
</form>

返回结果:
在这里插入图片描述
注意:
如果你用postman测试接口。
那么,@RequestBody收参时,对应的传参方式如下:
在这里插入图片描述
如果你想用postman传递form-data参数,就必须要用@RequestParam注解,并且key要对应上。

5、@CookieValue

作用说明:获取cookie参数

案例:
接口收参形式:

    @GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@CookieValue("_ga") String _ga,@CookieValue("_ga") Cookie cookie){Map<String,Object> map = new HashMap<>();map.put("_ga",_ga);System.out.println(cookie.getName()+"===>"+cookie.getValue());return map;}

请求传参形式:

http://127.0.0.1:8080/car/3/owner/lisi?age=18&inters=basketball&inters=game

浏览器添加Cookie
在这里插入图片描述
Postman添加cookie
在这里插入图片描述
返回结果:

{"_ga": "123456789"
}

6、@RequestAttribute

作用说明:页面转发时,用于获取request属性。

案例:
接口收参形式:
请求接口

    @GetMapping("/goto")public String goToPage(HttpServletRequest request){request.setAttribute("msg","成功了...");request.setAttribute("code",200);return "forward:/success";  //转发到  /success请求}

转发接口

    @ResponseBody@GetMapping("/success")public Map success(@RequestAttribute(value = "msg",required = false) String msg,@RequestAttribute(value = "code",required = false)Integer code,HttpServletRequest request){Object msg1 = request.getAttribute("msg");Map<String,Object> map = new HashMap<>();map.put("reqMethod_msg",msg1);map.put("annotation_msg",msg);return map;}

请求传参形式:

http://127.0.0.1:8080/goto

在这里插入图片描述

返回结果:

{"reqMethod_msg": "成功了...","annotation_msg": "成功了..."
}

7、@MatrixVariable

作用说明:获取矩阵参数,如果浏览器禁用了cookie,那么,就用它传递cookie参数。

案例:
开启矩阵参数功能:
在配置类中,向IOC容器注册一个WebMvcConfigurer实例。

    @Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {UrlPathHelper urlPathHelper = new UrlPathHelper();// 不移除;后面的内容。矩阵变量功能就可以生效urlPathHelper.setRemoveSemicolonContent(false);configurer.setUrlPathHelper(urlPathHelper);}};}

接口收参形式:

形式1//1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd//2、SpringBoot默认是禁用了矩阵变量的功能//      手动开启:原理。对于路径的处理。UrlPathHelper进行解析。//              removeSemicolonContent(移除分号内容)支持矩阵变量的//3、矩阵变量必须有url路径变量才能被解析@GetMapping("/cars/{path}")public Map carsSell(@MatrixVariable("low") Integer low,@MatrixVariable("brand") List<String> brand,@PathVariable("path") String path){Map<String,Object> map = new HashMap<>();map.put("low",low);map.put("brand",brand);map.put("path",path);return map;}形式2// /boss/1;age=20/2;age=10@GetMapping("/boss/{bossId}/{empId}")public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,@MatrixVariable(value = "age",pathVar = "empId") Integer empAge){Map<String,Object> map = new HashMap<>();map.put("bossAge",bossAge);map.put("empAge",empAge);return map;}

请求传参形式:

形式1
http://127.0.0.1:8080/cars/sell;low=34;brand=byd,audi,yd
http://127.0.0.1:8080/cars/sell;low=34;brand=byd;brand=audi;brand=yd
形式2
http://127.0.0.1:8080/boss/1;age=20/2;age=10

返回结果:

结果1
{"path": "sell","low": 34,"brand": ["byd","audi","yd"]
}
结果2
{"bossAge": 20,"empAge": 10
}

这篇关于SpringBoot2:请求处理原理分析-接口参数的常用注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1140289

相关文章

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot整合TDengine全过程

《springboot整合TDengine全过程》:本文主要介绍springboot整合TDengine全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录环境准备JDBC-JNI方式准备依赖实体类Mapper配置类测试类RESTful方式实体类配置类测试类总结

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文