使用 注解+AOP+redis 实现幂等接口
时间:2022-05-11 13:05
由于业务需要,会根据入参DTO中的某几个属性实现幂等接口,特此记录.
1.增加注解
ApiIdempotent
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
public @interface ApiIdempotent {
//幂等
/*
* 单位 秒
*/
long time() default 1;
/*
* 是否抛出异常
*/
boolean throwEx() default true;
}
增加注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Documented
public @interface ApiIdempotentField {
}
AOP部分逻辑
@Aspect
@Component
@Slf4j
public class ApiIdempotentInterceptor {
private RedisService redisService;
@Pointcut("@annotation(com.sky.common.base.annotation.ApiIdempotent)")
public void apiIdempotentPoint() {}
@Around(value = "apiIdempotentPoint() && @annotation(apiIdempotent)")
public Object methodAround(ProceedingJoinPoint jp,ApiIdempotent apiIdempotent) throws Throwable,IllegalAccessException {
Object[] pointArgs = jp.getArgs();
List