Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
59.4. Client Request Filter
Overview 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section explains how to implement and register a client request filter, which is used to intercept an outgoing request message on the client side. Client request filters are often used to process headers and can be used for any kind of generic request processing.
ClientRequestFilter interface 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
javax.ws.rs.client.ClientRequestFilter interface is defined as follows:
By implementing the
ClientRequestFilter, you can create a filter for the ClientRequest extension point on the client side, which filters the request message before sending the message to the server.
ClientRequestContext interface 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
filter method of ClientRequestFilter receives a single argument of type javax.ws.rs.client.ClientRequestContext, which can be used to access the outgoing request message and its related metadata. The ClientRequestContext interface is defined as follows:
Sample implementation 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
To implement a client request filter for the
ClientRequest extension point (that is, where the filter is executed prior to sending the request message), define a class that implements the ClientRequestFilter interface.
For example, the following code shows an example of a simple client request filter that gets installed in the
ClientRequest extension point, with a priority of 20:
Aborting the invocation 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
It is possible to abort a client-side invocation by implementing a suitable client request filter. For example, you might implement a client-side filter to check whether a request is correctly formatted and, if necessary, abort the request.
The following test code always aborts the request, returning the
BAD_REQUEST HTTP status to the client calling code:
Registering the client request filter 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Using the JAX-RS 2.0 client API, you can register a client request filter directly on a
javax.ws.rs.client.Client object or on a javax.ws.rs.client.WebTarget object. Effectively, this means that the client request filter can optionally be applied to different scopes, so that only certain URI paths are affected by the filter.
For example, the following code shows how to register the
SampleClientRequestFilter filter so that it applies to all invocations made using the client object; and how to register the TestAbortClientRequestFilter filter, so that it applies only to sub-paths of rest/TestAbortClientRequest.