Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 35 web apps ReaderInterceptor This type of interceptor is a contract extension interface provided by the JAX RS API On the server side reader interceptors act on HTTP payloads sent by the client while on the client side reader interceptors are supposed to act on read and mutate the request payload prior to it being sent to the server side WriterInterceptor This type of interceptor on the server side acts on HTTP payloads produced by the resource methods while on the client side a writer interceptor is supposed to act on read and mutate the payload sent by the server prior to it being dispatched to the caller This code shows the serverside variant in operation public interface WriterInterceptor public void aroundWriteFrom WriterInterceptorContext writerCtx throws IOException WebApplicationException JAX RS interceptors are invoked in a chain like fashion similar to filters and are triggered only when entity providers javax ws rs MessageBodyReader and javax ws rs MessageBodyWriter are required to convert HTTP messages to and from their Java object representations Both ReaderInterceptor and WriterInterceptor wrap around MessageBodyReader and MessageBodyWriter respectively Hence they are executed in the same call stack Binding Strategies for Filters and Interceptors JAX RS defines multiple ways that filters and interceptors can be bound to their target components For server side filters there is the global default behavior plus named binding and dynamic binding Lets look at these briefly Global default behavior By default JAX RS filters and interceptors are bound to all the methods of the resource classes in an application That is both request prematching and postmatching and response filters will be invoked whenever any resource method is invoked in response to an HTTP request by the client This convention can be overridden by using named binding or dynamic binding Named binding To handle filter and interceptor scoping in a fine grained manner on a per resource class or per method basis you can leverage the @ NameBinding annotation Doing this involves several steps Step 1 Define a custom annotation with the @ NameBinding annotation @ NameBinding @ Target ElementType TYPE ElementType METHOD @ Retention value RetentionPolicy RUNTIME public @ interface Audited Step 2 Apply the custom annotation to the filter or interceptor @ Provider @ Audited public class AuditFilter implements ContainerRequestFilter filter implementation Step 3 Apply the same annotation to the required resource JAX RS 20 includes a brand new API for asynchronous processing that includes server side as well as client side counterparts Being asynchronous inherently implies request processing on a different thread than the thread that initiated the request
You must have JavaScript enabled to view digital editions.