Feign 注解翻译器 三
一、自定义注解翻译器
(1)JAXRS 注解翻译器实例
① 导入JAXRS所需要的jar包
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>9.5.0</version>
</dependency> <dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>9.5.0</version>
</dependency> <!--配置xml客户端-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jaxb</artifactId>
<version>9.5.0</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency> <dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency> <!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<!--Feign 对 JAXRS 的支持-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jaxrs</artifactId>
<version>9.5.0</version>
</dependency> <!--JAXRS-->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
② 编写接口类 并使用 javax注解
import javax.ws.rs.GET;
import javax.ws.rs.Path; public interface RsClient { @GET
@Path(value = "/hello")
public String hello();
}
③ 测试方法
public static void main(String[] args) {
//使用 JAXRS 注解翻译器
RsClient rsClient = Feign.builder()
.contract(new JAXRSContract())
.target(RsClient.class, "http://localhost:8080");
String result = rsClient.hello();
System.out.println("result:"+result);
//使用自定义的注解翻译器
ContractClient client = Feign.builder().contract(new MyContract())
.target(ContractClient.class, "http://localhost:8080");
String result1 = client.hello();
System.out.println("result1:" + result1);
//设置请求拦截器
ClientInterface helloClient = Feign.builder()
.requestInterceptor(new MyInterceptor())
.target(ClientInterface.class, "http://localhost:8080");
String hello = helloClient.hello();
System.out.println(hello);
}
(2)自定义注解翻译器MyContract
① 自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyUrl { String url(); String method();
}
② 编写接口类 ContractClient 并使用自定义注解 @MyUrl
public interface ContractClient {
@MyUrl(url = "/hello", method = "GET")
public String hello();
}
③ 自定义注解翻译器
import feign.Contract;
import feign.MethodMetadata; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; public class MyContract extends Contract.BaseContract {
@Override
protected void processAnnotationOnClass(MethodMetadata methodMetadata, Class<?> aClass) { } @Override
protected void processAnnotationOnMethod(MethodMetadata methodMetadata, Annotation annotation, Method method) {
//注解是MyUrl类型的,才处理
if (MyUrl.class.isInstance(annotation)) {
MyUrl myUrl = method.getAnnotation(MyUrl.class);
String url = myUrl.url();
String httpMethod = myUrl.method();
methodMetadata.template().method(httpMethod);
//将接口地址追加到 url后面
methodMetadata.template().append(url); }
} @Override
protected boolean processAnnotationsOnParameter(MethodMetadata methodMetadata, Annotation[] annotations, int i) {
return false;
}
}
二、编写并设置请求拦截器(此处测试demo 上述main方法 使用的接口demo是 上一篇博客 Feign 二中的 ClientInterface)
(1)自定义请求拦截器
import feign.RequestInterceptor;
import feign.RequestTemplate; public class MyInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
//此处可以设置请求的数据类, 这样就不用再每个方法是上设置了
//@Headers("Content-Type: application/xml")
requestTemplate.header("Content-Type", "application/json");
System.out.println("这是请求拦截器");
}
}
三、设置接口请求日志
import com.idelan.cloud.interfaces.ClientInterface;
import feign.Feign;
import feign.Logger; public class HelloMain {
public static void main(String[] args) {
/**
* 日志级别描述
* NONE, 默认值,不记录日志
* BASIC, 记录请求方法,URL, 响应代码和执行时间
* Headers, 除了BASIC 记录的日志外,还会记录请求头和响应头的信息
* FULL,在HEADERS的基础上,请求响应的元数据,都会保存
*/
ClientInterface helloClient = Feign.builder()
.logLevel(Logger.Level.FULL)
.logger(new Logger.JavaLogger().appendToFile("E:/logs/http.log"))
.target(ClientInterface.class, "http://localhost:8080"); String hello = helloClient.hello();
System.out.println(hello);
}
}
Feign 注解翻译器 三的更多相关文章
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)
Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口并用注解来配置它既可完成对Web服务接 ...
- spring注解之@Import注解的三种使用方式
目录 1.@Import注解须知 2.@Import的三种用法 3.@Import注解的三种使用方式总结 @ 1.@Import注解须知 1.@Import只能用在类上 ,@Import通过快速导入的 ...
- SpringMVC 中 @ControllerAdvice 注解的三种使用场景!
@ControllerAdvice ,很多初学者可能都没有听说过这个注解,实际上,这是一个非常有用的注解,顾名思义,这是一个增强的 Controller.使用这个 Controller ,可以实现三个 ...
- Java 系列之spring学习--注解(三)
一.注解 使用注解之前要开启自动扫描功能 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- SpringMVC常用注解(三)
一.@Controller .@RestController 和 @ControllerAdvice 1. @Controller @Controller 用于标记在一个类上,使用它标记的类就是一个S ...
- 注解深入浅出之Retrofit中的注解(三)
更多andorid高级架构进阶视频免费分享学习请点击:https://space.bilibili.com/474380680 Retrofit中的注解 @Query,@QueryMap,@Field ...
- hibernate注解(三)1+N问题
一.什么时候会遇到1+N的问题? 前提:Hibernate默认表与表的关联方法是fetch="select",不是fetch="join",这都是为了懒加载而准 ...
- Java注解(三)
上一篇了解了自定义注解的使用,不过里面的例子没有多大使用价值,这一回来个有用点的Demo. 目标:将实体bean保存到数据库 先来定义一个实体注解 import java.lang.annotatio ...
随机推荐
- 关于Apache Commons-IO的使用
commons-io是一款处理io流的工具,封装了很多处理io流和文件的方法,可以大大简化我们处理io流和操作文件的代码.从common-io的官方使用文档可以看出,它主要分为工具类.尾端类.行迭代器 ...
- 微软不将《帝国时代》终极版上架Steam的原因到底是什么?
毋庸置疑的是,<帝国时代>绝对是一款经典游戏.作为一款RTS名作,在过去的20年时间中<帝国时代>销量超过2000万部.数以千万计的玩家都沉溺于这款游戏中,<帝国时代&g ...
- JQuery实现复制数据到剪贴板之各种麻花与右键点击弹出选择菜单
1.如果小伙伴们只是想实现点击某个按钮(通过click事件)实现复制功能. 那小哥哥我在这里推荐大家使用2个非常好用的插件 (1)clipboard.js:纯js插件,无需flash,相对来说更轻量级 ...
- Struts2加载自定义库注意事项
新建Struts2项目,添加Struts2的jar包时,往往通过导入自定义库的方式,导入自定义库时,有个地方必须要设置,否则项目无法正常执行,如图所示: 必须要按照上述方式对自定义库进行加载!
- CPU内核、用户模式
本文由是阅读该文章做下的笔记. CPU分内核与用户模式. 三言蔽之 内核模式下,应用可以直接存取内存,能够执行任何CPU指令.一般来说驱动运行在该模式下.内核模式的应用一旦崩溃,整个操作系统都会崩溃. ...
- 3dmax2015卸载/安装失败/如何彻底卸载清除干净3dmax2015注册表和文件的方法
3dmax2015提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dmax2015失败提示3dmax2015安装未完成,某些产品无法安装,也有时候想重新安装3 ...
- 视觉SLAM算法框架解析(3) SVO
版权声明:本文为博主原创文章,未经博主允许不得转载. SVO(Semi-direct Visual Odometry)[1]顾名思义是一套视觉里程计(VO)算法.相比于ORB-SLAM,它省去了回环检 ...
- MOOC(7)- case依赖、读取json配置文件进行多个接口请求-封装mock(9)
封装mock # -*- coding: utf-8 -*- # @Time : 2020/2/12 8:51 # @File : mock_demo_9.py # @Author: Hero Liu ...
- tfjs-node初体验:训练模型的存储
JS,一门从浏览器兴起,却不止于浏览器的脚本,个人一直认为其是最有潜力的脚本语言.不只是因为ES6优雅的语法,更重要的是其易上手,跨平台的优点. Node将JS从browser带去了client是革命 ...
- 在Docker内安装jenkins运行和基础配置
这里是在linux环境下安装docker之后,在doucer内安装jenkins --------------------docker 安装 jenkins---------------------- ...