如果你在拦截器中什么都不做,那么就不会发起HTTP调用,也就意味着不会获取连接。
进行 HTTP 调用的代码在InterceptingClientHttpRequest:
private class InterceptingRequestExecution implements ClientHttpRequestExecution {
private final Iterator<ClientHttpRequestInterceptor> iterator;
public InterceptingRequestExecution() {
this.iterator = interceptors.iterator();
}
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
if (this.iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, body, this);
}
else {
HttpMethod method = request.getMethod();
Assert.state(method != null, "No standard HTTP method");
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
if (body.length > 0) {
if (delegate instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream));
}
else {
StreamUtils.copy(body, delegate.getBody());
}
}
return delegate.execute();
}
}
}
如您所见,所有拦截器都被迭代以调用intercept。
一般情况下,在您的intercept method 中,您需要执行请求(如果您不想阻止它)。
假设你有两个拦截器:
RestTemplate rest = new RestTemplate();
rest.setInterceptors(Arrays.asList(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
System.out.println("first interceptor enter");
ClientHttpResponse execute = execution.execute(request, body);
System.out.println(execute.getStatusCode() + " In first interceptor");
System.out.println("first interceptor exit");
return execute;
}
}, new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
System.out.println("second interceptor enter");
ClientHttpResponse execute = execution.execute(request, body);
System.out.println(execute.getStatusCode() + " In second interceptor");
System.out.println("second interceptor exit");
return execute;
}
}));
return rest.getForObject("https://stackoverflow.com/", String.class);
// output:
first interceptor enter
second interceptor enter
200 OK In second interceptor
second interceptor exit
200 OK In first interceptor
first interceptor exit
这里的流程是:
- 第一个拦截器(
interceptor A)的拦截器方法被调用。
- 当
interceptor A 执行execution.execute(request, body) 时,会调用第二个拦截器(interceptor A)。
- 当
interceptor B执行execution.execute(request, body)时,由于没有拦截器,最终会通过requestFactory.createRequest(request.getURI(), method) 进行HTTP调用。 (在这种方法中,会如您所说的获取连接)。