@Provider public class TracingClientFilter extends Object implements javax.ws.rs.client.ClientRequestFilter, javax.ws.rs.client.ClientResponseFilter
for any client request invoked from the client API the client request filters
ClientRequestFilter are executed that could manipulate the request.
If not aborted, the outgoing request is then physically sent over to the server
side and once a response is received back from the server the client response
filters (ClientResponseFilter) are executed that might again manipulate the
returned response. Finally the response is passed back to the code that invoked
the request. If the request was aborted in any client request filter then the
client/server communication is skipped and the aborted response is used in the
response filters.
Note that there is also a concept of Interceptors in jax-rs specification. But they are more connected with the marshalling and unmarshalling of the HTTP message bodies that are contained in the requests and the responses. Note that they are executed after the filters and only if a message body is present. Hence it is decided to use client filter as they are more suitable for this purpose.
The client request filter will always create a new span to pass the newly
created span context (namely the traceId, spanId and the baggage items, if any)
to the next service on the chain. The same span would then be closed later by
the TracingClientFilter once it receives the response from the
remote service. User, however, can override the default
behavior by specifying the system property -Djax.rs.span
as false. In which case, no new span will be created, but only the contextual
data will be passed.
The filter has to be explicitly registered with the Client first.
Example:
.....
public Client restClient() {
Client client = ClientBuilder
.newBuilder()
.register(new TracingClientFilter())
.build();
}
....
....
public void getName() {
Client client = restClient();
client.target("url")
.request()
.get(String.class)
}
You can also register the request filter via ClientConfig as a provider
and use it like below:
.....
public Client restClient() {
ClientConfig config = new ClientConfig();
config.register(TracingClientFilter.class);
return ClientBuilder.newClient(config);
}
....
....
public void getName() {
Client client = restClient();
client.target("url")
.request()
.get(String.class)
}
| Constructor and Description |
|---|
TracingClientFilter() |
| Modifier and Type | Method and Description |
|---|---|
void |
filter(javax.ws.rs.client.ClientRequestContext requestCtx) |
void |
filter(javax.ws.rs.client.ClientRequestContext requestCtx,
javax.ws.rs.client.ClientResponseContext responseCtx) |
public void filter(javax.ws.rs.client.ClientRequestContext requestCtx)
throws IOException
filter in interface javax.ws.rs.client.ClientRequestFilterIOExceptionpublic void filter(javax.ws.rs.client.ClientRequestContext requestCtx,
javax.ws.rs.client.ClientResponseContext responseCtx)
throws IOException
filter in interface javax.ws.rs.client.ClientResponseFilterIOExceptionCopyright © 2020. All rights reserved.