兄弟们,相信遇到过重复请求的痛点吧,我也遇过,因此,写了一个自定义注解去解决这个问题,接下来看代码。
首先:创建一个自定义注解 RequestLimit 。然后字段的话是 second、maxCount。看代码
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Description 请求限流/防刷自定义注解
* @date 2022/04/05 18:20
* @author by TheSky
*/
@Documented
@Inherited
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {
// 在 second 秒内,最大只能请求 maxCount 次
int second() default 1;
int maxCount() default 1;
}
接下来就是拦截器了,创建拦截器: RequestLimitIntercept 继承 HandlerInterceptorAdapter
看代码:
import com.alibaba.fastjson.JSONObject;
imp