Feign 自定义编码器、解码器和客户端
Feign 的编码器、解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编写自己的编码器和解码器,如果希望编写自己的编码器,需要实现 feign.codec.Encoder 接口,解码器需要实现 feign.codec.Decoder 接口,示例如下:
自定义编码器和解码器
- 自定义编码器
实现的自定义编码器只是输出了需要编码的参数信息,而具体的编码还是使用 JSON 格式,使用了 GsonEncoder 编码器来完成具体的编码工作,参数 object 表示需要进行编码的对象,参数 bodyType 为 object 对象的类型,参数 template 表示的就是请求模板,该方法就是需要实现将参数 object 进行编码并赋值到 template 请求模板中。
package org.lixue.feignclient;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.gson.GsonEncoder;
import java.lang.reflect.Type;
public class MyEncoder implements Encoder{
private GsonEncoder gsonEncoder;
publicMyEncoder(){
gsonEncoder = new GsonEncoder();
}
public void encode(Object object,Type bodyType,RequestTemplate template) throws EncodeException{
System.out.println("encode object is class"+object.getClass().getName());
System.out.println("encode object is value"+object);
System.out.println("encode bodyType is class"+bodyType.getClass().getName());
System.out.println("encode bodyType is value"+bodyType);
gsonEncoder.encode(object,bodyType,template);
}
}
- 自定义解码器
实现的自定义解码器使用了 GsonDecoder 解码器来完成具体的编码工作,解码器相对简单,只需要从响应中获取响应报文,然后按照指定的编码格式相应的解码并创建指定的类型实例即可。
package org.lixue.feignclient;
import feign.FeignException;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import feign.gson.GsonDecoder;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class MyDecoder implements Decoder{
private GsonDecoder gsonDecoder;
publicMyDecoder(){
gsonDecoder=newGsonDecoder();
}
public Object decode(Response response,Type type)throws IOException,DecodeException,FeignException{
return gsonDecoder.decode(response,type);
}
}
- 测试验证
在完成自定义编码器和解码器的开发后,只需要在 Feign 的 builder 方法中,增加解码器和编码器即可,需要注意的是,如果方法请求参数或返回的不是对象,不需要进行编码或解码,就不能增加编码器或解码器,示例代码如下:
package org.lixue.feignclient;
import feign.Feign;
import feign.Logger;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
public class Startup{
public static void main(String[]args){
HelloWorldClient speakClient=
Feign.builder().target(HelloWorldClient.class,"http://localhost:8080/");
// 参数和返回都不是对象,不需要附加编码器和解码器
System.out.println(speakClient.speak("isbody"));
HelloWorldClient findByIdClient=
Feign.builder().decoder(new GsonDecoder())
.target(HelloWorldClient.class,"http://localhost:8080/");
// 返回的是对象,需要附加解码器
Person person=findByIdClient.findById(34);
System.out.println("personid="+person.getId()+"age="+person.getAge()+"name="+person.getName()+"message="+person.getMessage());
HelloWorldClient createClient=
Feign.builder().client(newMyClient())
.decoder(newMyDecoder())
.encoder(newMyEncoder())
.target(HelloWorldClient.class,"http://localhost:8080/");
Person newPerson=new Person();
newPerson.setId(3434);
newPerson.setAge(34);
newPerson.setName("343434");
newPerson.setMessage("33333333333333333");
// 参数和返回都是对象,需要附加解码器和编码器
ReturnValuereturnValue=createClient.create(newPerson);
System.out.println(returnValue.parseString());
}
}
自定义 Feign 客户端
- 自定义 Feign 客户端
Feign 使用一个 feign.Client 接口来发送请求,默认实现是使用 HttpURLConnection 连接 HTTP 服务,我们可以实现 feign.Client 接口来完成自定义 Feign 客户端的开发,该接口只有一个方法 execute ,用于执行请求,下面实现了一个自定义的 Feign 客户端,主要完成了请求的日志记录,示例代码如下:
package org.lixue.feignclient;
import feign.Client;
import feign.Request;
import feign.Response;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
public class MyClient implements Client{
public Response execute(Request request,Request.Options options)throws IOException{
System.out.println("execute request method="+request.method());
System.out.println("execute request headers");
Map<String,Collection<String>> headers=request.headers();
for(Map.Entry<String,Collection<String>> entry:headers.entrySet()){
StringBuilderstringBuilder=newStringBuilder();
for(intj=0;j<entry.getValue().size();j++){
if(stringBuilder.length()>0){
stringBuilder.append(",");
}
stringBuilder.append(entry.getValue());
}
System.out.println(entry.getKey()+":"+stringBuilder.toString());
}
byte[] body=request.body();
if(body!=null){
System.out.println("execute request body="+newString(body));
}
// 使用 Feign 默认的客户端请求
return new Client.Default(null,null).execute(request,options);
}
}
- 测试验证
和附加编码器、解码器类似,只需要在 Feign 的 builder 方法中附加自定义的客户端即可,代码如下:
package org.lixue.feignclient;
import feign.Feign;
import feign.Logger;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
public class Startup{
public static void main(String[]args){
HelloWorldClient findByIdClient=
Feign.builder().client(new MyClient())
.decoder(new GsonDecoder())
.target(HelloWorldClient.class,"http://localhost:8080/");
Person person=findByIdClient.findById(34);
System.out.println("personid="+person.getId()+"age="+person.getAge()+"name="+person.getName()+"message="+person.getMessage());
}
}
Feign 自定义编码器、解码器和客户端的更多相关文章
- Feign 自定义编码器、解码器和客户端,Feign 转发请求头(header参数)、Feign输出Info级别日志
Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...
- Netty学习笔记(三) 自定义编码器
编写一个网络应用程序需要实现某种编解码器,编解码器的作用就是讲原始字节数据与自定义的消息对象进行互转.网络中都是以字节码的数据形式来传输数据的,服务器编码数据后发送到客户端,客户端需要对数据进行解码, ...
- Feign自定义编程配置
介绍 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feigh默认使用的编码器.解码器.所使用的契约等.Spring Cloud允 ...
- Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践
Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 目录 Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 引子 FeignClient的 ...
- python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器
python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 ...
- 普适注意力:用于机器翻译的2D卷积神经网络,显著优于编码器-解码器架构
现有的当前最佳机器翻译系统都是基于编码器-解码器架构的,二者都有注意力机制,但现有的注意力机制建模能力有限.本文提出了一种替代方法,这种方法依赖于跨越两个序列的单个 2D 卷积神经网络.该网络的每一层 ...
- 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器
低延迟.全接口(HMDI.DVI.YPb Pr.RGB)H.264全高清编码器解码器 一.产品介绍 1.近零延时的H.264压缩到1920x1080p60 该产品提供分辨率为1920x1080p6 ...
- 【干货】如何通过OPC自定义接口来实现客户端数据的读取?
上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...
- 自定义token,保存到客户端的cookie中,
自定义token #原理自定义token,放入cookie中,不用存数据库 #token定义方式 >>>>> "加密字符串"|登陆用户id|用户登陆时 ...
随机推荐
- MySQL数据库优化的方式
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...
- 2.24 js处理内嵌div滚动条
2.24 js处理内嵌div滚动条 前言 前面有篇专门用js解决了浏览器滚动条的问题,生活总是多姿多彩,有的滚动条就在页面上,这时候又得仰仗js大哥来解决啦.一.内嵌滚动条 1.下面这张图 ...
- hadoop day 1
hadoop是什么? 解决的问题: 海量数据的存储(HDFS):供hbase,mapreduce进行处理 海量数据的分析(MapReduce) 资源管理调度(YARN) 搜索引擎: 爬虫系统+站内搜索 ...
- 【err】VIDEOIO ERROR: V4L: index 0 is not correct!Unable to connect to camera
前言 新到手一块板子,程序编译成功之后,运行出现错误,不能连接到摄像头. 问题 VIDEOIO ERROR: V4L: index is not correct! Unable to connect ...
- pytorch使用tensorboardX进行网络可视化
我们知道,对于pytorch上的搭建动态图的代码的可读性非常高,实际上对于一些比较简单的网络,比如alexnet,vgg阅读起来就能够脑补它们的网络结构,但是对于比较复杂的网络,如unet,直接从代码 ...
- c# 移动鼠标到指定位置
/// <summary> /// 引用user32.dll动态链接库(windows api), /// 使用库中定义 API:SetCursorPos /// </summary ...
- PowerDesigner15 增加Domain域
第一步: 第二步: 点击此按钮,在弹出框中对Domain域打钩即可
- Linux如何在(localhost)本地打开html
自己实验的结果,可能不是最好的办法,方法不唯一 1. 下载Tomcat https://www.cnblogs.com/liangweiping/p/5113857.html 如何查看本机的端口服务 ...
- 从简单的mongodb example 的观察
https://github.com/no7dw/mongodb-example 这是最基础的连接查询.(branch master) var MongoClient = require('mongo ...
- Go Example--打点器
package main import ( "time" "fmt" ) func main() { // 定时器 是当你想要在未来某一刻执行一次时使用的 - ...