Java最新Springboot实现防重复提交和防重复点击(附源码),Java技术功底不够如何去面试

总结

虽然我个人也经常自嘲,十年之后要去成为外卖专员,但实际上依靠自身的努力,是能够减少三十五岁之后的焦虑的,毕竟好的架构师并不多。

架构师,是我们大部分技术人的职业目标,一名好的架构师来源于机遇(公司)、个人努力(吃得苦、肯钻研)、天分(真的热爱)的三者协作的结果,实践+机遇+努力才能助你成为优秀的架构师。

如果你也想成为一名好的架构师,那或许这份Java成长笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • @param testId 测试id

  • @param requestVo 请求参数

  • @return

  • @author daleyzou

*/

@PostMapping(“/test/{testId}”)

@NoRepeatSubmit(location = “thisIsTestLocation”, seconds = 6)

public RsVo thisIsTestLocation(@PathVariable Integer testId, @RequestBody RequestVo requestVo) throws Throwable {

// 睡眠 5 秒,模拟业务逻辑

Thread.sleep(5);

return RsVo.success(“test is return success”);

}

2、根据接口收到的 RequestBody 中指定变量名的值判断为一

Copy/**

  • 根据请求参数里的 RequestBody 里获取指定名称的变量param5的值进行接口级别防重复点击

  • @param testId 测试id

  • @param requestVo 请求参数

  • @return

  • @author daleyzou

*/

@PostMapping(“/test/{testId}”)

@NoRepeatSubmit(location = “thisIsTestBody”, seconds = 6, argIndex = 1, name = “param5”)

public RsVo thisIsTestBody(@PathVariable Integer testId, @RequestBody RequestVo requestVo) throws Throwable {

// 睡眠 5 秒,模拟业务逻辑

Thread.sleep(5);

return RsVo.success(“test is return success”);

}

ps: jedis 2.9 和 springboot有各种兼容问题,无奈只有降低springboot的版本了

运行结果#

=========

Copy收到响应:{“succeeded”:true,“code”:500,“msg”:“操作过于频繁,请稍后重试”,“data”:null}

收到响应:{“succeeded”:true,“code”:500,“msg”:“操作过于频繁,请稍后重试”,“data”:null}

收到响应:{“succeeded”:true,“code”:500,“msg”:“操作过于频繁,请稍后重试”,“data”:null}

收到响应:{“succeeded”:true,“code”:200,“msg”:“success”,“data”:“test is return success”}

测试用例#

=========

Copypackage com.dalelyzou.preventrepeatsubmit.controller;

import com.dalelyzou.preventrepeatsubmit.PreventrepeatsubmitApplicationTests;

import com.dalelyzou.preventrepeatsubmit.service.AsyncFeginService;

import com.dalelyzou.preventrepeatsubmit.vo.RequestVo;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

/**

  • TestControllerTest

  • @description 防重复点击测试类

  • @author daleyzou

  • @date 2020年09月28日 17:13

  • @version 1.3.1

*/

class TestControllerTest extends PreventrepeatsubmitApplicationTests {

@Autowired

AsyncFeginService asyncFeginService; @Test

public void thisIsTestLocation() throws IOException {

RequestVo requestVo = new RequestVo();

requestVo.setParam5(“random”);

ExecutorService executorService = Executors.newFixedThreadPool(4);

for (int i = 0; i <= 3; i++) {

executorService.execute(() -> { String kl = asyncFeginService.thisIsTestLocation(requestVo); System.err.println(“收到响应:” + kl);

}); } System.in.read(); } @Test

public void thisIsTestBody() throws IOException {

RequestVo requestVo = new RequestVo();

requestVo.setParam5(“special”);

ExecutorService executorService = Executors.newFixedThreadPool(4);

for (int i = 0; i <= 3; i++) {

executorService.execute(() -> { String kl = asyncFeginService.thisIsTestBody(requestVo); System.err.println(“收到响应:” + kl);

}); } System.in.read(); }}

定义一个注解#

===========

Copypackage com.dalelyzou.preventrepeatsubmit.aspect;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

  • NoRepeatSubmit

  • @description 重复点击的切面

  • @author daleyzou

  • @date 2020年09月23日 14:35

  • @version 1.4.8

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface NoRepeatSubmit {

/**

  • 锁过期的时间

  • */

int seconds() default 5;

/**

  • 锁的位置

  • */

String location() default “NoRepeatSubmit”;

/**

  • 要扫描的参数位置

  • */

int argIndex() default 0;

/**

  • 参数名称

  • */

String name() default “”;

}

根据指定的注解定义一个切面,根据参数中的指定值来判断请求是否重复#

=================================

Copypackage com.dalelyzou.preventrepeatsubmit.aspect;

import com.dalelyzou.preventrepeatsubmit.constant.RedisKey;

import com.dalelyzou.preventrepeatsubmit.service.LockService;

import com.dalelyzou.preventrepeatsubmit.vo.RsVo;

import com.google.common.collect.Maps;

import com.google.gson.Gson;

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.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

import java.util.Map;

@Aspect

@Component

public class NoRepeatSubmitAspect {

private static final Logger logger = LoggerFactory.getLogger(NoRepeatSubmitAspect.class);

private static Gson gson = new Gson();

private static final String SUFFIX = “SUFFIX”;

最后

终极手撕架构师的学习笔记:分布式+微服务+开源框架+性能优化

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

g SUFFIX = “SUFFIX”;

最后

终极手撕架构师的学习笔记:分布式+微服务+开源框架+性能优化

[外链图片转存中…(img-ekQDRJyA-1715413803819)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值