推荐链接: ????总结——》【Java】 ????总结——》【Mysql】 ????总结——》【Spring】 ????总结——》【SpringBoot】
一、数据库
CREATE
DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `user`
VALUES (1, '赵');
INSERT INTO `user`
VALUES (2, '钱');
INSERT INTO `user`
VALUES (3, '孙');
INSERT INTO `user`
VALUES (4, '李');
二、项目
参考代码: 【gitee】https://gitee.com/xiaoxianwansui/springboot-request-limit.git
1、pom.xml:引入核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2、application.yml:配置数据源
3、RequestLimit.java:定义注解
4、RequestLimitAspect.java:定义AOP切面
5、RequestLimitException.java:定义注解异常
6、Application.java:使用@EnableAspectJAutoProxy注解
7、UserController.java:使用@RequestLimit注解
三、启动
四、测试
1、正常返回结果
http://127.0.0.1:8888/user/list
2、抛出异常信息
http://127.0.0.1:8888/user/list
|