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

Spring Boot 注解探秘:HTTP 请求的魅力之旅

在SpringBoot应用开发中,处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程,使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理,同时也提升了代码的可读性和维护性。

一、@RequestMapping

@RequestMapping用于将特定的HTTP请求映射到特定的方法上。可用于类级别和方法级别上,以下是代码示例。

@RestController
@RequestMapping("/customer-api/") // 类级别,所有方法都以customer-api开头
public class  CustomerApi{@RequestMapping(value = "/get-customer-by-id", method = RequestMethod.GET){ // 方法级别,当收到对/customer-api/get-customer-by-id路径的GET请求时,会调用getCustomerById方法。Response<Customer> getCustomerById(@RequestParam("id") Integer id)……}
}

本节示例中@RestController和@RequestParam注解可以先忽略,下面会介绍到。

二、PutMapping、DeleteMapping、GetMapping和PostMapping

为了更加明确表示不同的HTTP方法,Spring Boot提供了一组特定的注解,分别对应PUT, DELETE、GET、POST(增删改查)请求。

@RestController
@RequestMapping("/customer-api/") 
public class  CustomerApi{@PutMapping("/add-customer") // 增加的请求,和@RequestMapping的PUT方式等价Response<?> addCustomer(@RequestBody Customer customer)……}@DeleteMapping("/delete-customer-by-id") // 删除的请求,和@RequestMapping的DELETE方式等价Response<?> deleteCustomer(@RequestParam("id") Integer id)……}@PostMapping("/update-customer") // 更新的请求,和@RequestMapping的POST方式等价Response<?> updateCustomer(@RequestBody Customer customer)……}// @RequestMapping(value = "/get-customer-by-id", method = RequestMethod.GET)   // 方式1@GetMapping("/get-customer-by-id") // 和方式1是等价的。Response<Customer> getCustomerById(@RequestParam("id") Integer id)……}
}

三、@RequestParam和@PathVariable

  • @RequestParam用于获取查询参数
  • @PathVariable用于获取路径变量
@GetMapping("/get-customer-by-id"){ Response<Customer> getCustomerById(@RequestParam("id") Integer id) // @RequestParam就可获取请求路径/get-customer-by-id?id=1中的id的值……}@GetMapping("/get-customer/{id}"){ Response<Customer> getCustomerById(@PathVariable("id") Integer id) // @PathVariable可获取路径/get-customer/{id}中的id的值……}

四、@RequestBody

@RequestBody注解用于将 HTTP 请求的主体内容绑定到方法的参数上。通常用于处理 POST 和 PUT 请求,当请求的主体是 JSON 或 XML 格式的数据时非常有用。
示例见第二节,这里就不重复赘述了。

五、@RestController和Controller

@RestController和@Controller都是用于定义控制器类的注解,但是两者之间有细微的差异。
@RestController是一个组合注解,相当于@Controller和@ResponseBody。 用@RestController标注的API类,其中的方法会直接返回数据(如JSON、XML),不会返回视图。
@Controller刚好相反,它标注的类中的方法会直接返回视图(如JSP、Thymeleaf模版等)。

  • 小结:现在的微服务项目基本都是前后端分离,所以@Controller已经慢慢的淡出了视野,很少使用,而@RestController已然成为了主流。

后面有时间了在聊聊GET和POST请求的区别,大厂面试被问到的频次贼高。

http://www.lryc.cn/news/432086.html

相关文章:

  • TYPE-C USB设计
  • Python炒股自动化,怎样理解股票交易性质
  • Vue2 day-02
  • 什么?!新版 Node.js V22.5 自带 SQLite 模块啦
  • Maven持续集成(Continuous integration,简称CI)版本友好管理
  • EvoSuite使用总结
  • Cortex-A7:简单中断处理(不可嵌套中断)机制
  • k8s HPA
  • 5G移动网络运维实验(训)室解决方案
  • 单片机学习笔记
  • SpringBoot中@Value获取值和@ConfigurationProperties获取值用法及比较
  • 执行任务赚积分
  • 使用TLS解决Docker API暴露2375端口的问题
  • Pyspark中catalog的作用与常用方法
  • 聚焦2024数博会|与天空卫士一起探索AI与数据安全的融合应用
  • 实战docker第二天——cuda11.8,pytorch基础环境docker打包
  • 企业数字化转型的利器:RFID资产管理系统
  • matplotlib中文乱码问题
  • 提高开发效率的实用工具库VueUse
  • 【数据结构】你真的学会了二叉树了吗,来做一做二叉树的算法题及选择题
  • 压力测试知识总结
  • @import导入样式以及scss变量应用与static目录
  • 分类中的语义一致性约束:助力模型优化
  • 前端框架介绍
  • java基础知识-JVM知识详解
  • 流动会场:以声学专利为核心的完美移动场地—轻空间
  • 深度学习(一)-感知机+神经网络+激活函数
  • 目标检测-YOLOv4
  • 一台笔记本电脑的硬件都有哪些以及对应的功能
  • 【程序分享1】第一性原理计算 + 数据处理程序