Java Reference
In-Depth Information
Given that the database of our online preregistration application will contain thou-
sands of students, the following code demonstrates how we can take advantage of
interceptors in the data exchange with the Ministry of Education in order to avoid net-
work overloading when transmitting information about students.
The following code shows the implementation of WriterInterceptor (on the
server side) that will compress data to send to the JAX-RS client. The @ZipResult
annotation allows us to bind the interceptor only to some JAX-RS resources. If we
remove this annotation, all JAX-RS resources of our application will be automatically
compressed.
The following code is an example of a WriterInterceptor implemention:
@ZipResult
@Provider
public class MyGzipWriterJaxRsInterceptor
implements WriterInterceptor{
@Override
public void
aroundWriteTo(WriterInterceptorContext wic)
throws IOException {
try (GZIPOutputStream gzipStream = new
GZIPOutputStream(wic.getOutputStream());) {
wic.setOutputStream(gzipStream);
wic.proceed();
}
}
}
To bind the MyGzipWriterJaxRsInterceptor interceptor to a resource, we will
only decorate the given resource with the @ZipResult annotation. The following
code demonstrates how to bind MyGzipWriterJaxRsInterceptor interceptor to
a resource so that its representation can be always compressed before being sent
to the client.
The following code is an example of of interceptor binding:
Search WWH ::




Custom Search