一、自定义注解翻译器

(1)JAXRS 注解翻译器实例

  ① 导入JAXRS所需要的jar包

  1. <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>9.5.0</version>
    </dependency>
  2.  
  3. <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-gson</artifactId>
    <version>9.5.0</version>
    </dependency>
  4.  
  5. <!--配置xml客户端-->
    <dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-jaxb</artifactId>
    <version>9.5.0</version>
    </dependency>
  6.  
  7. <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
    </dependency>
  8.  
  9. <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
    </dependency>
  10.  
  11. <!--httpclient-->
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
    </dependency>
  1.   
  2.  
  3. <!--Feign 对 JAXRS 的支持-->
  4. <dependency>
  5. <groupId>io.github.openfeign</groupId>
  6. <artifactId>feign-jaxrs</artifactId>
  7. <version>9.5.0</version>
  8. </dependency>
  9.  
  10. <!--JAXRS-->
  11. <dependency>
  12. <groupId>javax.ws.rs</groupId>
  13. <artifactId>jsr311-api</artifactId>
  14. <version>1.1.1</version>
  15. </dependency>

② 编写接口类 并使用 javax注解

  1. import javax.ws.rs.GET;
  2. import javax.ws.rs.Path;
  3.  
  4. public interface RsClient {
  5.  
  6. @GET
  7. @Path(value = "/hello")
  8. public String hello();
  9. }

③ 测试方法

  1. public static void main(String[] args) {
  2. //使用 JAXRS 注解翻译器
  3. RsClient rsClient = Feign.builder()
  4. .contract(new JAXRSContract())
  5. .target(RsClient.class, "http://localhost:8080");
  6. String result = rsClient.hello();
  7. System.out.println("result:"+result);
  8.  
  9. //使用自定义的注解翻译器
  10. ContractClient client = Feign.builder().contract(new MyContract())
  11. .target(ContractClient.class, "http://localhost:8080");
  12. String result1 = client.hello();
  13. System.out.println("result1:" + result1);
  14.  
  15. //设置请求拦截器
  16. ClientInterface helloClient = Feign.builder()
  17. .requestInterceptor(new MyInterceptor())
  18. .target(ClientInterface.class, "http://localhost:8080");
  19.  
  20. String hello = helloClient.hello();
  21. System.out.println(hello);
  22. }

(2)自定义注解翻译器MyContract

  ① 自定义注解

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface MyUrl {
  4.  
  5. String url();
  6.  
  7. String method();
  8. }

② 编写接口类 ContractClient 并使用自定义注解 @MyUrl

  1. public interface ContractClient {
  2.  
  3. @MyUrl(url = "/hello", method = "GET")
  4. public String hello();
  5. }

③ 自定义注解翻译器

  1. import feign.Contract;
  2. import feign.MethodMetadata;
  3.  
  4. import java.lang.annotation.Annotation;
  5. import java.lang.reflect.Method;
  6.  
  7. public class MyContract extends Contract.BaseContract {
  8. @Override
  9. protected void processAnnotationOnClass(MethodMetadata methodMetadata, Class<?> aClass) {
  10.  
  11. }
  12.  
  13. @Override
  14. protected void processAnnotationOnMethod(MethodMetadata methodMetadata, Annotation annotation, Method method) {
  15. //注解是MyUrl类型的,才处理
  16. if (MyUrl.class.isInstance(annotation)) {
  17. MyUrl myUrl = method.getAnnotation(MyUrl.class);
  18. String url = myUrl.url();
  19. String httpMethod = myUrl.method();
  20. methodMetadata.template().method(httpMethod);
  21. //将接口地址追加到 url后面
  22. methodMetadata.template().append(url);
  23.  
  24. }
  25. }
  26.  
  27. @Override
  28. protected boolean processAnnotationsOnParameter(MethodMetadata methodMetadata, Annotation[] annotations, int i) {
  29. return false;
  30. }
  31. }

二、编写并设置请求拦截器(此处测试demo 上述main方法 使用的接口demo是 上一篇博客 Feign 二中的 ClientInterface)

(1)自定义请求拦截器

  1. import feign.RequestInterceptor;
  2. import feign.RequestTemplate;
  3.  
  4. public class MyInterceptor implements RequestInterceptor {
  5. @Override
  6. public void apply(RequestTemplate requestTemplate) {
  7. //此处可以设置请求的数据类, 这样就不用再每个方法是上设置了
  8. //@Headers("Content-Type: application/xml")
  9. requestTemplate.header("Content-Type", "application/json");
  10. System.out.println("这是请求拦截器");
  11. }
  12. }

三、设置接口请求日志

  1. import com.idelan.cloud.interfaces.ClientInterface;
  2. import feign.Feign;
  3. import feign.Logger;
  4.  
  5. public class HelloMain {
  6. public static void main(String[] args) {
  7. /**
  8. * 日志级别描述
  9. * NONE, 默认值,不记录日志
  10. * BASIC, 记录请求方法,URL, 响应代码和执行时间
  11. * Headers, 除了BASIC 记录的日志外,还会记录请求头和响应头的信息
  12. * FULL,在HEADERS的基础上,请求响应的元数据,都会保存
  13. */
  14. ClientInterface helloClient = Feign.builder()
  15. .logLevel(Logger.Level.FULL)
  16. .logger(new Logger.JavaLogger().appendToFile("E:/logs/http.log"))
  17. .target(ClientInterface.class, "http://localhost:8080");
  18.  
  19. String hello = helloClient.hello();
  20. System.out.println(hello);
  21. }
  22. }

Feign 注解翻译器 三的更多相关文章

  1. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  2. Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)

    Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口并用注解来配置它既可完成对Web服务接 ...

  3. spring注解之@Import注解的三种使用方式

    目录 1.@Import注解须知 2.@Import的三种用法 3.@Import注解的三种使用方式总结 @ 1.@Import注解须知 1.@Import只能用在类上 ,@Import通过快速导入的 ...

  4. SpringMVC 中 @ControllerAdvice 注解的三种使用场景!

    @ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...

  5. Java 系列之spring学习--注解(三)

    一.注解 使用注解之前要开启自动扫描功能 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  6. SpringMVC常用注解(三)

    一.@Controller .@RestController 和 @ControllerAdvice 1. @Controller @Controller 用于标记在一个类上,使用它标记的类就是一个S ...

  7. 注解深入浅出之Retrofit中的注解(三)

    更多andorid高级架构进阶视频免费分享学习请点击:https://space.bilibili.com/474380680 Retrofit中的注解 @Query,@QueryMap,@Field ...

  8. hibernate注解(三)1+N问题

    一.什么时候会遇到1+N的问题? 前提:Hibernate默认表与表的关联方法是fetch="select",不是fetch="join",这都是为了懒加载而准 ...

  9. Java注解(三)

    上一篇了解了自定义注解的使用,不过里面的例子没有多大使用价值,这一回来个有用点的Demo. 目标:将实体bean保存到数据库 先来定义一个实体注解 import java.lang.annotatio ...

随机推荐

  1. 百度2019校招Web前端工程师笔试卷(第二批)

    一.linux系统下有多个文件目录,每个文件目录都有其独特的功能和作用 /bin 存放普通用户可以使用的指令. /usr 这个目录中包含了命令库文件和在通常操作中不会修改的文件,其地位类似Window ...

  2. 02-Java开发环境的配置

    在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www ...

  3. [LC] 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. kafka spark steam 写入elasticsearch的部分问题

    应用版本 elasticsearch 5.5 spark 2.2.0 hadoop 2.7 依赖包版本 docker cp /Users/cclient/.ivy2/cache/org.elastic ...

  5. SCI|EI|ISTP|万方|istic|NSTL|CASTD|CNKI|nlc|ethesys|CALIS|CETD|proquest|NDLTD|中国科学院学位论文检索系统|学位论文

    BD AC D 三大检索指的是:SCI(科学引文索引 ).EI(工程索引 ).ISTP(科技会议录索引 ) 即Science Citation Index.Engineering Index.Conf ...

  6. 最初级的ajax程序

    该文章实现的ajax功能是实现了在<span>上面添加内容 jsp代码 <html><head><title>Ajax</title>< ...

  7. [LC] 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  8. SpringBoot之HandlerInterceptor拦截器的使用 ——(三)获取requestBody解决java.io.IOException: Stream closed

    原文地址:https://blog.csdn.net/zhibo_lv/article/details/81875705 感谢原作者

  9. 安装centos7后不能联网

    我们在安装centos的minimal版本后,在使用yum安装工具时会提示:cannot find a valid baseurl or repo:base/7/x86_64 这是因为不能联网导致的, ...

  10. struts2和springmvc性能比较2

    我们用struts2时采用的传统的配置文件的方式,并没有使用传说中的0配置.spring3 mvc可以认为已经100%零配置了(除了配置spring mvc-servlet.xml外). Spring ...