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

SpringBoot 3.0 - 自定义注解+拦截器+Redis 解决接口幂等性

在这里插入图片描述

01 依赖注入

确保你的 pom.xml 文件中精准罗列以下关键依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

02 自定义注解

自定义注解 @IdempotentToken

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public  @interface  IdempotentToken {long    expireTime() default 60; // 设置默认过期时间为 60 秒String  message() default "重复请求"; // 设置默认重复请求提示信息
}

注解:

  1. 通过 @Target 指定注解可用范围,使其既能修饰方法又能修饰类

  2. 借助 @Retention 确定注解的生命周期,使其能在运行时被读取利用

  3. 内置可灵活配置的过期时间及提示信息参数,适配多样化业务场景需求

03 拦截器打造

拦截器 IdempotencyInterceptor,使其成为拦截请求、严谨校验幂等性 :

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;@Component
publicclassIdempotencyInterceptor   implements   HandlerInterceptor {@Autowiredprivate  StringRedisTemplate   redisTemplate;@OverridepublicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {if (handler instanceof HandlerMethod) {HandlerMethodhandlerMethod= (HandlerMethod) handler;Methodmethod= handlerMethod.getMethod();IdempotentTokenidempotentToken= method.getAnnotation(IdempotentToken.class );if (idempotentToken != null) {StringuniqueKey= generateUniqueKey(request); // 生成唯一业务标识Stringkey="idempotent:" + uniqueKey;Long   expireTime= idempotentToken.expireTime();Stringvalue= redisTemplate.opsForValue().get(key);if (value == null) {redisTemplate.opsForValue().set(key, "1", expireTime, TimeUnit.SECONDS); // 初次请求,设置计数与过期时间} else {intcount= Integer .parseInt(value);if (count >= 3) {response.setStatus(HttpServletResponse.SC_FORBIDDEN);response.getWriter().write(idempotentToken.message());returnfalse; // 阻断重复请求,返回定制化提示} else {redisTemplate.opsForValue().increment(key); // 合法请求,计数加 1}}}}returntrue;}private  String  generateUniqueKey(HttpServletRequest request) {// 综合用户信息、请求参数、请求路径等多维度信息生成唯一键值StringBuilderuniqueKeyBuilder=newStringBuilder();uniqueKeyBuilder.append(request.getMethod()).append(":");uniqueKeyBuilder.append(request.getRequestURI()).append(":");// 可按需追加更多细分参数,提升唯一性保障精度return  uniqueKeyBuilder.toString();}
}

注解:

  1. 依托 Spring 的依赖注入机制,无缝整合 Redis 操作模板,为后续缓存交互提供便捷通路

  2. 在拦截器的核心方法 preHandle 内,精准识别标记了 @IdempotentToken 注解的方法

  3. 借助唯一业务标识生成策略,巧妙融合请求方法、路径等关键信息,确保每次业务操作对应独立的幂等性校验维度

  4. 对初次合法请求严谨设置过期时间与初始计数,对重复请求依据计数阈值果断阻断,同时返回贴合业务的定制化提示信息

04配置适配

配置类 WebConfig 中精心注册拦截器,使其全面接管系统请求的幂等性管控:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
publicclassWebConfigimplementsWebMvcConfigurer {@Autowiredprivate  IdempotencyInterceptor idempotencyInterceptor;@OverridepublicvoidaddInterceptors(InterceptorRegistry registry) {registry.addInterceptor(idempotencyInterceptor).addPathPatterns("/**"); // 指定拦截器管控范围,可灵活按需细化路径匹配规则}
}

05控制器应用

业务控制器方法上轻松点缀 @IdempotentToken 注解,即刻赋予其幂等性防护能力:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/comment")
publicclassCommentController {@PostMapping("/add")@IdempotentToken(expireTime = 30, message = "短时间内评论过于频繁")public  String  addComment(@RequestBody CommentRequest commentRequest) {// 业务逻辑处理代码return"评论添加成功";}
}

通过以上层层递进、环环相扣的精心设计与实现,

我们成功借助 Spring Boot 3.0 的强大生态,巧妙融合自定义注解的灵活性、拦截器的管控力以及 Redis 的高效缓存特性,

为系统接口打造了一套坚固耐用、贴合实战的幂等性解决方案。

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

相关文章:

  • 【apache-maven3.9安装与配置】
  • 从虚拟机角度解释python3相对导入问题(下)
  • 轻量化实物建模革命:WebGL如何实现复杂模型的高效加载与交互
  • ​CentOS 7 单用户模式重置 root 密码完整指南
  • 新中国风通用读书颂词分享PPT模版
  • JS核心操作符:从基础到ES6+
  • (ICML-2023)BLIP-2:使用冻结图像编码器与大型语言模型的语言-图像预训练引导方法
  • SQL Server 查询数据库及数据文件大小
  • 使用 spark-submit 运行依赖第三方库的 Python 文件
  • RGB相机 vs 灰度相机
  • Apache Flink Kafka 写连接器源码深度剖析
  • java-SpringBoot框架开发计算器网页端编程练习项目【web版】
  • Drag-and-Drop LLMs: Zero-Shot Prompt-to-Weights
  • DataSophon 1.2.1集成Flink 1.20并增加JMX 监控
  • pyqt setContentsMargins
  • 网络安全攻防:2025年新型钓鱼攻击防御指南
  • 零基础搭建Spring AI本地开发环境指南
  • LT8311EX一款适用于笔记本电脑,扩展坞的usb2.0高速运转芯片,成对使用,延伸长度达120米
  • 202564读书笔记|《土耳其:换个地方躺平(轻游记)》——旅行的时候,绮丽多姿的真实世界向我打开
  • Python核心库Pandas详解:数据处理与分析利器
  • 【Java开发日记】我们详细地讲解一下 Java 异常及要如何处理
  • Springboot项目中使用手机号短信验证码注册登录实现
  • Vue项目使用defer优化页面白屏,性能优化提升,秒加载!!!
  • 【服务器】教程 — Linux上如何挂载服务器NAS
  • 帮助装修公司拓展客户资源的微信装修小程序怎么做?
  • STM32 环境监测与控制系统的设计与实现
  • Vue3+el-table-v2虚拟表格大数据量多选功能详细教程
  • STM32[笔记]--4.嵌入式硬件基础
  • 攻防世界-MISC-MeowMeowMeow
  • Unity小工具:资源引用的检索和替换