使用方式
HttpClient支持通过builder配置和SPI加载两种方式配置Interceptor。Builder配置
在构造HttpClient时传入自定义的Interceptor实例,如:
final HttpClient client = HttpClient.create().addInterceptor((request, next) -> {
System.out.println("Interceptor");
return next.proceed(request);
}).build();
SPI
HttpClient支持通过SPI的方式加载Interceptor接口的实现类,使用时只需要按照SPI的加载规则将自定义的Interceptor放入指定的目录下即可。
Note
-
使用时可以通过
Interceptor替换原始的HttpRequest和HttpResponse; 2.HttpClient内置的重试、重定向、100-expect-continue协商等功能通过Interceptor实现。 -
多个拦截器之间通过
getOrder()方法返回值区分执行顺序,值越小,优先级越高。
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.
Last modified February 18, 2022: complete document migration (#126) (1375641)