当前位置: 代码网 > it编程>编程语言>Java > SpringBoot接口防抖的5种高效方案

SpringBoot接口防抖的5种高效方案

2026年01月27日 Java 我要评论
前言接口防抖是一种用于限制用户重复提交请求的机制。在web开发中,用户可能会因为网络延迟或者多次点击按钮而导致多次提交同一个请求,这可能会对系统产生不必要的压力,或者导致数据异常。接口防抖就是为了解决

前言

接口防抖是一种用于限制用户重复提交请求的机制。在web开发中,用户可能会因为网络延迟或者多次点击按钮而导致多次提交同一个请求,这可能会对系统产生不必要的压力,或者导致数据异常。接口防抖就是为了解决这个问题而设计的。

接口防抖的基本原理是,在接收到一个请求后,服务器会在一定的时间内暂时忽略后续相同的请求,直到这段时间过去,才会再次处理新的请求。这样可以有效地避免重复提交导致的问题。

常见的接口防抖实现方式包括基于缓存、基于token、基于拦截器等。在spring boot中,我们可以利用拦截器、过滤器或者切面等机制来实现接口防抖功能。

下面我们就来看看在springboot中如何实现这些操作。

基于缓存实现防抖

这里使用了concurrenthashmap作为缓存来实现,当然在实际操作的时候还可以使用redis缓存或者是memcached来作为缓存操作。

@component
public class debounceinterceptor {
 
    private static final concurrenthashmap<string, long> cache = 
      new concurrenthashmap<>();
    private static final long expire_time = 5000; // 5秒内重复请求会被拦截
 
    public boolean shouldintercept(httpservletrequest request) {
        string key = request.getmethod() + ":" + request.getrequesturi() + ":" 
          + request.getparametermap();
        long now = system.currenttimemillis();
        if (cache.containskey(key)) {
            long lastrequesttime = cache.get(key);
            if (now - lastrequesttime < expire_time) {
                return true; // 请求被拦截
            }
        }
        cache.put(key, now);
        return false; // 请求通过
    }
}

在上面的代码中,我们使用concurrenthashmap来存储请求信息,其中key是请求的方法、url和参数,value是请求的时间戳。如果同样的请求在5秒内重复出现,则会被拦截。然后,我们创建一个拦截器来应用这个防抖逻辑,如下所示。

public class debounceinterceptor implements handlerinterceptor {
 
    @autowired
    private debounceservice debounceservice;
 
    @override
    public boolean prehandle(httpservletrequest request, httpservletresponse response, 
                             object handler) throws exception {
        if (debounceservice.shouldintercept(request)) {
               // 返回429状态码表示请求过多
            response.setstatus(httpservletresponse.sc_too_many_requests); 
            return false;
        }
        return true;
    }
}

最后,在配置类中注册这个拦截器。如下所示。

@configuration
public class webmvcconfig implements webmvcconfigurer {
 
    @override
    public void addinterceptors(interceptorregistry registry) {
        registry.addinterceptor(new debounceinterceptor()).addpathpatterns("/**");
    }
}

基于token实现防抖

通过在请求中添加token,在一定时间内禁止相同的token重复提交,代码如下所示。

@component
public class tokeninterceptor {
 
    private static final concurrenthashmap<string, long> cache = 
      new concurrenthashmap<>();
    private static final long expire_time = 5000; // 5秒内重复token会被拦截
 
    public boolean shouldintercept(httpservletrequest request) {
        string token = request.getheader("x-request-token");
        if (stringutils.isempty(token)) {
            return false; // 请求通过,没有token
        }
        long now = system.currenttimemillis();
        if (cache.containskey(token)) {
            long lastrequesttime = cache.get(token);
            if (now - lastrequesttime < expire_time) {
                return true; // 请求被拦截
            }
        }
        cache.put(token, now);
        return false; // 请求通过
    }
}

在这种方法中,我们需要在请求头中添加一个token,然后使用concurrenthashmap来存储token和请求时间的映射关系。如果相同的token在5秒内重复出现,则会被拦截。然后,我们创建一个拦截器来应用这个防抖逻辑,与上面的方法类似。

基于注解实现防抖

通过自定义注解来实现防抖,使得只需要在需要防抖的接口上添加注解即可。

定义注解类

@target(elementtype.method)
@retention(retentionpolicy.runtime)
public @interface debounce {
    long value() default 5000; // 默认5秒内重复请求会被拦截
}

创建切面类来实现拦截注解的操作。

@aspect
@component
public class debounceaspect {
 
    private static final concurrenthashmap<string, long> cache = 
      new concurrenthashmap<>();
 
    @autowired
    private httpservletrequest request;
 
    @around("@annotation(debounce)")
    public object debounce(proceedingjoinpoint joinpoint, debounce debounce) 
throws throwable {
        string key = request.getmethod() + ":" + request.getrequesturi() + ":" 
          + request.getparametermap();
        long now = system.currenttimemillis();
        if (cache.containskey(key)) {
            long lastrequesttime = cache.get(key);
            if (now - lastrequesttime < debounce.value()) {
                return null; // 请求被拦截
            }
        }
        cache.put(key, now);
        return joinpoint.proceed(); // 请求通过
    }
}

最后,在启动类上添加@enableaspectjautoproxy注解,启用切面代理功能。

总结

以上是几种常见的spring boot接口防抖的实现方案,你可以根据具体情况选择适合你的方案,并根据需求进行定制和扩展。

到此这篇关于springboot接口防抖的5种高效方案的文章就介绍到这了,更多相关springboot接口防抖内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com