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

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

一、@RequestMapping

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

java 复制代码
@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(增删改查)请求。

java 复制代码
@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用于获取路径变量
java 复制代码
	@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请求的区别,大厂面试被问到的频次贼高。

相关推荐
鱼跃鹰飞31 分钟前
设计模式系列:工厂模式
java·设计模式·系统架构
a努力。44 分钟前
国家电网Java面试被问:混沌工程在分布式系统中的应用
java·开发语言·数据库·git·mysql·面试·职场和发展
Yvonne爱编码1 小时前
Java 四大内部类全解析:从设计本质到实战应用
java·开发语言·python
J2虾虾1 小时前
SpringBoot和mybatis Plus不兼容报错的问题
java·spring boot·mybatis
毕设源码-郭学长2 小时前
【开题答辩全过程】以 基于springboot 的豪华婚车租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Loo国昌3 小时前
深入理解 FastAPI:Python高性能API框架的完整指南
开发语言·人工智能·后端·python·langchain·fastapi
Tao____3 小时前
通用性物联网平台
java·物联网·mqtt·低代码·开源
曹轲恒4 小时前
SpringBoot整合SpringMVC(上)
java·spring boot·spring
JH30734 小时前
Java Spring中@AllArgsConstructor注解引发的依赖注入异常解决
java·开发语言·spring
码农水水5 小时前
米哈游Java面试被问:机器学习模型的在线服务和A/B测试
java·开发语言·数据库·spring boot·后端·机器学习·word