Enterprise Java

Adding Gzip Compression in CXF APIs and Interceptors

Nowadays it has become mandatory to perform Gzipping to the APIs responses, due to huge amount of data we are sending in the response. It saves network bandwidth and delivery time, and of course space over the internet.

CXF provides an option to use the Gzip Compression in a number of ways.

  1. Blueprint
  2. Annotation

 
 
 
 
 
 

Blueprint:

1
2
3
4
5
6
<bean id="gZipInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" />
    <jaxrs:server id="rsServer" address="/gZip">
        <jaxrs:outInterceptors>
            <ref component-id="gZipInterceptor" />
        </jaxrs:outInterceptors>
    </jaxrs:server>

Annotation:

First you need to register the GZIPOutInterceptor in out interceptors list. For that you need to hook into CXF initialization classes.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
public class InterceptorManager extends AbstractFeature {
 
    private static final Logger LOGGER = Logger.getLogger( "simcore" );
    private static final Interceptor< Message > GZIP = new GZIPOutInterceptor();
    //private static final Interceptor< Message > GZIP = new GZIPOutInterceptor(512);
     
    /* (non-Javadoc)
     * @see org.apache.cxf.feature.AbstractFeature#initializeProvider(org.apache.cxf.interceptor.InterceptorProvider, org.apache.cxf.Bus)
     */
    @Override
    protected void initializeProvider( InterceptorProvider provider, Bus bus ) {
        /**
         * Adding Gzip interceptor to all outbound requests/responses
         */
        LOGGER.debug( " ##############  Adding Gzip as OUT Interceptor ##############" );
         
        provider.getOutInterceptors().add( GZIP );
     
    }
}

GZIPOutInterceptor comes with an option to set the Threshold value as no of Bytes. If the response size is below this threshold value, then it will not be compressed. This will be extremely useful when we will be sending empty lists and status messages/codes only, since compressing those small responses will be an overhead at server side.

But there is another factor we must look into, which is the number of users requesting the response. So, set this value appropriately by thinking over all the cases that might appear.

1
@GZIP

Now we can use this annotation on any of our web-services controller to implement compression on all the APIs provided in that class.

01
02
03
04
05
06
07
08
09
10
11
12
@WebService
@Consumes ( { MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
@Produces ( MediaType.APPLICATION_JSON )
@GZIP
public interface WebServicesController {
 
        @GET
    @Path ( "/myGzipData" )
    @Produces ( { MediaType.APPLICATION_JSON } )
    Response getZipData( );
 
}

Moreover we can set different parameters in Gzip annotation.

1
@GZIP ( force = true, threshold = 512 )
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nick
Nick
8 years ago

When implementing this solution, im getting this error when deploying my code on a weblogic server:

org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute ‘component-id’ is not allowed to appear in element ‘ref’.

Back to top button