Spring Boot 自定义注解

文章目录

  • Spring Boot 自定义注解
    • 目标
    • 实现
      • 加依赖
      • 加配置
      • 写代码
        • 定义自定义注解(含参数的)
        • 实现注解(获取参数)
        • Controller
      • 看效果

Spring Boot 自定义注解

目标

弄个自定义注解

实现

加依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-lang3artifactId>
dependency>

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
dependency>



<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjrtartifactId>
dependency>

<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
dependency>

<dependency>
    <groupId>com.squareup.okhttp3groupId>
    <artifactId>okhttpartifactId>
    <version>4.3.1version>
dependency>

<dependency>
    <groupId>org.jetbrains.kotlingroupId>
    <artifactId>kotlin-stdlibartifactId>
    <version>1.6.21version>
dependency>

加配置

spring:
  main: 
    allow-bean-definition-overriding: true

写代码

定义自定义注解(含参数的)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamAnnotation {
    String value();
}

实现注解(获取参数)

@Slf4j
@Aspect
@Component
public class ParamAnnotationAspect {

    @Autowired
    private AnnotationService annotationService;

    @Around("@annotation(com.frank.custom.annotations.annotation.ParamAnnotation)")
    public Object intercept(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        ParamAnnotation annotation = method.getAnnotation(ParamAnnotation.class);
        String value = annotation.value();
        // 处理注解参数
        System.out.println("自定义注解的参数:" + value);

        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            log.info("Request parameters ----> {}", Arrays.toString(args));
        }

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String param = JSON.toJSONString(request.getParameterMap());
        System.out.println("请求参数:" + param);
        annotationService.saveLog();
        return joinPoint.proceed();
    }
}

Controller

@Slf4j
@RestController
@RequestMapping("/custom/annotations")
public class CustomAnnotationsController {

    @RequestMapping(value = "/test", method = RequestMethod.GET, name = "测试自定义注解")
    @ParamAnnotation(value = "我是个参数")
    public Result test(String name) {
        log.info("hello:{}", name);
        return Result.fail(HttpStatusEnum.SUCCESS);
    }

    @RequestMapping(value = "/param", method = RequestMethod.GET, name = "PARAM-测试自定义注解")
    @ParamAnnotation(value = "测试自定义参数接口")
    public Result param(TestVO testVO) {
        System.out.println("参数接口:"+JSONObject.toJSONString(testVO));
        return Result.fail(HttpStatusEnum.SUCCESS);
    }
}

看效果

就是请求Controller,然后控制台输出结果哈~~

END.

你可能感兴趣的:(spring,boot,后端,java,自定义注解)