api-idempotent接口幂等插件
快速上手
1. Maven依赖
在项目的 pom.xml
中添加以下依赖:
xml
<parent>
<groupId>com.yupaits</groupId>
<artifactId>yutool-parent</artifactId>
<version>${yutool.version}</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.yupaits</groupId>
<artifactId>api-idempotent</artifactId>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
2. RequestId获取接口
java
/**
* 默认的RequestId生成器
* @author yupaits
* @date 2020/4/26
*/
public class DefaultRequestIdGenerator implements RequestIdGenerator {
@Value("${id.generator.workerId:0}")
private long workerId;
@Value("${id.generator.datacenterId:0}")
private long datacenterId;
@Override
public String genRequestId() {
return new Snowflake(workerId, datacenterId, true).nextIdStr();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3. 在需要幂等校验的接口类或方法上使用@Idempotent注解
java
@RestController
@RequestMapping("/Animal")
@Api(tags = "动物接口")
public class AnimalController {
private final AnimalService animalService;
@Autowired
public AnimalController(AnimalService animalService) {
this.animalService = animalService;
}
@Idempotent
@ApiOperation("保存动物")
@PostMapping("")
public Result save(@RequestBody DtoWrapper<AnimalDto> dto) throws BusinessException {
return animalService.save(dto);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
评论区留言准则:
1. 本评论区禁止传播封建迷信、吸烟酗酒、低俗色情、赌博诈骗等任何违法违规内容。
2. 当他人以不正当方式诱导打赏、私下交易,请谨慎判断,以防人身财产损失。
3. 请勿轻信各类招聘征婚、代练代抽、私下交易、购买礼包码、游戏币等广告信息,谨防网络诈骗。