springcloud-eureka集群-整合feign框架自定义注解解释器

继之前项目,加入自定义注解解释器

1、创建自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String url();
    String method();
}

2、创建注解解释器,继承spring的注解解释器,增强对方法的注解解释

/**
 * 自定义注解解释器
 */
public class MyContract extends SpringMvcContract {

    /**
     * 重写spring框架中对方法注解的解释器
     * 对父类方法进行增强
     * @param data
     * @param annotation
     * @param method
     */
    @Override
    protected void processAnnotationOnMethod(MethodMetadata data, Annotation annotation, Method method){
        super.processAnnotationOnMethod(data, annotation, method);
        if(MyAnnotation.class.isInstance(annotation)){
            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
            String url = myAnnotation.url();
            String httpMethod = myAnnotation.method();
            data.template().method(httpMethod);
            data.template().append(url);
        }
    }
}

3、将自定义注解解释器加入spring容器

@Configuration
public class BaseConfig {

    @Bean
    public Contract feignContract(){
        return new MyContract();
    }

}

4、编写接口方法

/**
 * 调用自定义接口的注解
 * @return
 */
@MyAnnotation(url = "/hello", method = "GET")
String myAnnotationHello();

5、编写Controller方法

@GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String hello(){
    return iService.myAnnotationHello();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值