Feign的请求拦截器

一 定义拦截器

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

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81142934