Skip to content

dahui888/custom-annotation

 
 

Repository files navigation

自定义注解

模块介绍

  • custom-annotation-demo1:自定义校验注解
  • custom-annotation-demo2:自定义权限注解
  • custom-annotation-demo3:自定义缓存注解
  • custom-annotation-web:自定义注解测试

测试例子

校验注解测试

  • 实体类 User.java
 @Check(paramValues = {"man", "woman"})
 private String sex;

对需要校验的字段添加@Check注解

  • Controller层
@PostMapping("/api/test")
public Object test(@Validated @RequestBody User user) {
    return "hello world";
}
  • Postman模拟请求测试

不符合校验: 符合校验规则

符合校验规则: 符合校验规则

权限注解测试

  • Controller层
@GetMapping("/api/test")
@PermissionCheck(resourceKey = "test")
public Object testPermissionCheck() {
    return "hello world";
}
  • Postman模拟请求测试

权限校验通过

@PermissionCheck(resourceKey = "test")替换为@PermissionCheck(resourceKey = "testKey") 权限校验不通过

缓存注解测试

  • Controller层
@GetMapping("/api/cache")
@CustomCache(key = "test")
public Object testCustomCache() {
    return "don't hit cache";
}
  • Postman模拟请求测试

不命中缓存

@CustomCache(key = "test")替换为@CustomCache(key = "testKey") 命中缓存


了解更多:Spring自定义注解从入门到精通

About

自定义注解,包括了自定义校验注解、自定义缓存注解、自定义权限校验注解示例代码

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%