Feign的请求拦截器

  • Post author:
  • Post category:其他



一 定义拦截器

package org.crazyit.cloud.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;

public class MyInterceptor implements RequestInterceptor {

    public void apply(RequestTemplate template) {
        template.header("Content-Type", "application/json");
        System.out.println("这是自定义请求拦截器");
    }

}


二 测试拦截器类

package org.crazyit.cloud.interceptor;

import org.crazyit.cloud.HelloClient;

import feign.Feign;

public class InterceptorMain {

    public static void main(String[] args) {
        HelloClient client = Feign.builder()
                .requestInterceptor(new MyInterceptor())
                .target(HelloClient.class,
                "http://localhost:8080");
        String result = client.hello();
        System.out.println(result);
    }

}


三 启动服务


四 测试


这是自定义请求拦截器


Hello World



版权声明:本文为chengqiuming原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。