Implementing response message body interceptors

You may use the javax.ws.rs.ext.JAXRSWriterInterceptor interface to intercept and manipulate the outgoing message body. The following example illustrates the use of WriterInterceptor to compress the response body content:

//Other imports are omitted for brevity 
import java.util.zip.GZIPOutputStream; 
import javax.ws.rs.WebApplicationException; 
import javax.ws.rs.core.MultivaluedMap; 
import javax.ws.rs.ext.Provider; 
import javax.ws.rs.ext.WriterInterceptor; 
import javax.ws.rs.ext.WriterInterceptorContext; 
 
@Provider 
public class JAXRSWriterInterceptor implements WriterInterceptor { 
    @Override 
    public void aroundWriteTo(WriterInterceptorContext context) 
       throws IOException,  
       WebApplicationException { 
       MultivaluedMap<String, Object> headers =  
           context.getHeaders(); 
       headers.add("Content-Encoding", "gzip"); 
       OutputStream outputStream =  
           context.getOutputStream(); 
       context.setOutputStream(new  
            GZIPOutputStream(outputStream)); 
       context.proceed(); 
    } 
} 

You will use the same interface contract for building the write interceptors for the client side use as well. You can register the WriterInterceptor interface to the JAX-RS client by calling the register() method on the javax.ws.rs.client.Client object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.224.226