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.Este conteúdo não está disponível no idioma selecionado.
58.7. Entity Writer Interceptor
Overview Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
This section explains how to implement and register an entity writer interceptor, which enables you to intercept the output stream when writing a message body either on the client side or on the server side. This is typically useful for generic transformations of the request body, such as encryption and decryption, or compressing and decompressing.
WriterInterceptor interface Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The
javax.ws.rs.ext.WriterInterceptor
interface is defined as follows:
By implementing the
WriterInterceptor
interface, you can intercept the message body (Entity
object) as it is being written either on the server side or the client side. You can use an entity writer interceptor in either of the following contexts:
- Server side—if bound as a server-side interceptor, the entity writer interceptor intercepts the response message body just before it is marshalled and sent back to the client.
- Client side—if bound as a client-side interceptor, the entity writer interceptor intercepts the request message body just before it is marshalled and sent out to the server.
WriterInterceptorContext interface Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The
aroundWriteTo
method of WriterInterceptor
receives one argument of type javax.ws.rs.ext.WriterInterceptorContext
, which can be used to access both the message body (Entity
object) and message metadata.
The
WriterInterceptorContext
interface is defined as follows:
InterceptorContext interface Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The
WriterInterceptorContext
interface also supports the methods inherited from the base InterceptorContext
interface. For the definition of InterceptorContext
, see the section called “InterceptorContext interface”.
Sample implementation on the client side Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To implement an entity writer interceptor for the client side, define a class that implements the
WriterInterceptor
interface.
For example, the following code shows an example of an entity writer interceptor for the client side (with a priority of 10), which appends an extra line of text to the message body of the outgoing request:
Sample implementation on the server side Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To implement an entity writer interceptor for the server side, define a class that implements the
WriterInterceptor
interface and annotate it with the @Provider
annotation.
For example, the following code shows an example of an entity writer interceptor for the server side (with a priority of 10), which appends an extra line of text to the message body of the outgoing request:
Binding a writer interceptor on the client side Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
Using the JAX-RS 2.0 client API, you can register an entity writer interceptor directly on a
javax.ws.rs.client.Client
object or on a javax.ws.rs.client.WebTarget
object. Effectively, this means that the writer interceptor can optionally be applied to different scopes, so that only certain URI paths are affected by the interceptor.
For example, the following code shows how to register the
SampleClientReaderInterceptor
interceptor so that it applies to all invocations made using the client
object:
For more details about registering interceptors with a JAX-RS 2.0 client, see Section 46.5, “Configuring the Client Endpoint”.
Binding a writer interceptor on the server side Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To bind a writer interceptor on the server side (that is, to install it into the Apache CXF runtime), perform the following steps:
- Add the
@Provider
annotation to the writer interceptor class, as shown in the following code fragment:Copy to Clipboard Copied! Toggle word wrap Toggle overflow When the writer interceptor implementation is loaded into the Apache CXF runtime, the REST implementation automatically scans the loaded classes to search for the classes marked with the@Provider
annotation (the scanning phase). - When defining a JAX-RS server endpoint in XML (for example, see Section 16.1, “Configuring JAX-RS Server Endpoints”), add the writer interceptor to the list of providers in the
jaxrs:providers
element.Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThis step is a non-standard requirement of Apache CXF. Strictly speaking, according to the JAX-RS standard, the@Provider
annotation should be all that is required to bind the interceptor. But in practice, the standard approach is somewhat inflexible and can lead to clashing providers when many libraries are included in a large project.