防重复提交:自定义注解 + AOP(@Aspect)切面
一、思路:
1、自定义注解;
2、创建切面类;
二、代码示例:
1、自定义注解;
import java.lang.annotation.*;
/**
* @ClassName Resubmit
* @Descripition 自定义注解-防重复提交
* @Author
* @Date 2023/8/31 10:38
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Resubmit {
/**
* 默认过期时间
* 单位:秒
*
* @return
*/
int value() default 100;
/**
* 频繁请求提示语
*
* @return
*/
String messge() default "请求过于频繁,请稍后再试!";
}
2、创建切面类;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org

本文介绍了如何使用SpringAOP和自定义注解在Java应用中实现防重复提交功能,通过Redis存储请求频率,防止接口被频繁请求。
最低0.47元/天 解锁文章
1136

被折叠的 条评论
为什么被折叠?



