当前位置:网站首页>Customize to prevent repeated submission of annotations (using redis)
Customize to prevent repeated submission of annotations (using redis)
2022-06-25 11:35:00 【wo595】
Custom prevent duplicate submission comments ( utilize redis)
Custom annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** * Avoid duplicate submissions * @ClassName: AvoidRepeatableCommit */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AvoidRepeatableCommit {
/** * Cannot submit repeatedly within the specified time , Unit millisecond */
long timeout() default 3000 ;
}
AOP
import com.ljmes.vo.ResultUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/** * Repeated submission aop **/
@Aspect
@Component
public class AvoidRepeatableCommitAspect {
@Autowired
private RedisTemplate redisTemplate;
/** * @param point */
@Around("@annotation(com.ljmes.util.AvoidRepeatableCommit)")
public Object around(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
// Get access to url
StringBuffer requestURL = request.getRequestURL();
System.out.println("requestURL==" + requestURL);
// Get comments
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
// Parameters
StringBuilder params = new StringBuilder();
for (Object arg : point.getArgs()) {
params.append(arg);
}
System.out.println("params = " + params);
// Use the accessed url add params As key
String key = requestURL+params.toString();
System.out.println("key==" + key);
// Get custom annotations
AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class);
long timeout = avoidRepeatableCommit.timeout();
if (timeout < 0) {
timeout = 3;
}
// obtain redis The value in
Object obj = redisTemplate.opsForValue().get(key);
// Not for null Description in timeout Repeated submission within time
if (obj != null) {
return ResultUtil.error(" Repeated submission !");
}
redisTemplate.opsForValue().set(key, UUID.randomUUID().toString(), timeout, TimeUnit.MILLISECONDS);
// Execution method
Object object = point.proceed();
System.out.println("object==" + object);
return object;
}
}
Dependencies used
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
</project>
test
import com.ljmes.util.AvoidRepeatableCommit;
import com.ljmes.vo.ResultUtil;
import com.ljmes.vo.ResultVO;
import com.ljmes.vo.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
@Api("demo")
@RestController
@RequestMapping("/demo")
public class DemoController {
@ApiOperation(" Prevent duplicate submissions demo")
@PostMapping("/demo")
@AvoidRepeatableCommit
public ResultVO demo(@RequestBody UserDTO demo, @RequestParam Integer userId) {
return ResultUtil.success(demo);
}
}


边栏推荐
猜你喜欢

Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million

Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation

Design and implementation of university laboratory goods management information system based on SSH

Nacos installation and use

推荐一款M1电脑可用的虚拟机软件

Ladder Side-Tuning:预训练模型的“过墙梯”

记一次有趣的逻辑SRC挖掘

仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)

Spark runs wordcount (case 2)

Whole process of web page request
随机推荐
Jincang database kingbasees plug-in identity_ pwdexp
Kingbasees plug-in DBMS of Jincang database_ UTILITY
2022 PMP project management examination agile knowledge points (2)
兴业证券是国企吗?在兴业证券开户资金安全吗?
Comparator (for arrays.sort)
Kingbasees plug-in DBMS of Jincang database_ OUTPUT
Crawler scheduling framework of scratch+scratch+grammar
CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
Big endian and little endian
Handler、Message、Looper、MessageQueue
MySQL synchronous data configuration and shell script implementation
Ladder side tuning: the "wall ladder" of the pre training model
ARM64汇编的函数有那些需要注意?
SQL injection vulnerability (type chapter)
寿命分布 4种
ThingsPanel 发布物联网手机客户端(多图)
过拟合原因及解决
Jincang database kingbasees plug-in force_ view
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
【上云精品】节能提效!加速纺织业“智造”转型