一、自定义注解翻译器
(1)JAXRS 注解翻译器实例
① 导入JAXRS所需要的jar包
io.github.openfeign feign-core 9.5.0 io.github.openfeign feign-gson 9.5.0 io.github.openfeign feign-jaxb 9.5.0 org.projectlombok lombok 1.16.20 javax.xml.bind jaxb-api 2.3.0 org.apache.httpcomponents httpclient 4.5.5
io.github.openfeign feign-jaxrs 9.5.0 javax.ws.rs jsr311-api 1.1.1
② 编写接口类 并使用 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); }}