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 自定义编码器、解码器和客户端的更多相关文章

  1. Feign 自定义编码器、解码器和客户端,Feign 转发请求头(header参数)、Feign输出Info级别日志

    Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...

  2. Netty学习笔记(三) 自定义编码器

    编写一个网络应用程序需要实现某种编解码器,编解码器的作用就是讲原始字节数据与自定义的消息对象进行互转.网络中都是以字节码的数据形式来传输数据的,服务器编码数据后发送到客户端,客户端需要对数据进行解码, ...

  3. Feign自定义编程配置

    介绍 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feigh默认使用的编码器.解码器.所使用的契约等.Spring Cloud允 ...

  4. Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践

    Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 目录 Spring Cloud Feign 自定义配置(重试.拦截与错误码处理) 实践 引子 FeignClient的 ...

  5. python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器

    python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 ...

  6. 普适注意力:用于机器翻译的2D卷积神经网络,显著优于编码器-解码器架构

    现有的当前最佳机器翻译系统都是基于编码器-解码器架构的,二者都有注意力机制,但现有的注意力机制建模能力有限.本文提出了一种替代方法,这种方法依赖于跨越两个序列的单个 2D 卷积神经网络.该网络的每一层 ...

  7. 12-低延迟、全接口(HMDI、DVI、YPb Pr、RGB)H.264全高清编码器解码器

    低延迟.全接口(HMDI.DVI.YPb Pr.RGB)H.264全高清编码器解码器 一.产品介绍  1.近零延时的H.264压缩到1920x1080p60  该产品提供分辨率为1920x1080p6 ...

  8. 【干货】如何通过OPC自定义接口来实现客户端数据的读取?

    上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...

  9. 自定义token,保存到客户端的cookie中,

    自定义token #原理自定义token,放入cookie中,不用存数据库 #token定义方式 >>>>> "加密字符串"|登陆用户id|用户登陆时 ...

随机推荐

  1. python flask 小项目

    0 开始之前 网上看了很多教程,都不是很满意,因此自己写一个大型教程,从入门到做出一个比较完整的博客.此次教程不是直接把整个博客直接代码整理出来然后运行一遍就完事,我会从flask的各个模块讲起.所以 ...

  2. VB识别分隔符

    strTypeEx = ReadIniFile("Type", "Type", App.Path & "\set.ini") str ...

  3. python day02 作业答案

    1. (1).false   (2).false 2. (1).8  (2).4 3. (1).6  (2).3  (3).false (4).3   (5).true   (6).true  (7) ...

  4. 【Python】进程-锁(1)

    #第二题,做一个加减乘除的考试系统,自动出题,自动判对错,并统计结果,一次考试10道题 import random symbols=["+","-"," ...

  5. REST是什么?

    REST -- REpresentational State Transfer 直接翻译:表现层状态转移.   @Ivony 老师的一句话概括很精辟: 用URL定位资源,用HTTP动词(GET,POS ...

  6. Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块

    文章目录 1. 实战的开端 – Maven搭建 2. 参数的配置 - 属性参数类 3. 真的很简单 - 简单的服务类 4. 自动配置的核心 - 自动配置类 5. spring.factories 不要 ...

  7. 【linux】ARM板子开启浮点和neon加速

    参考 1. ARM平台NEON指令的编译和优化; 2. 交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别; 3. https://blog.csdn. ...

  8. pytorch实现rnn并且对mnist进行分类

    1.RNN简介 rnn,相比很多人都已经听腻,但是真正用代码操练起来,其中还是有很多细节值得琢磨. 虽然大家都在说,我还是要强调一次,rnn实际上是处理的是序列问题,与之形成对比的是cnn,cnn不能 ...

  9. C++中输出字符到文本文档

    #include <iostream> #include <fstream> //ofstream类的头文件 using namespace std; int main() { ...

  10. libsvm使用总结

    ./tools/ subset.py  分割数据集 grid.py   优化参数c.g checkdata.py   检测数据集格式 easy.py   综合 ./windows/ svm-scale ...