Java Reference
In-Depth Information
Adding filters
JAX-RS allows us to define filters and interceptors for both the client and server. They al-
low the developer to address cross-cutting concerns, such as security, auditing, or com-
pression. Basically, you can treat filters and interceptors as extension points.
Filters are used mainly on headers of requests and responses. For example, you can block
a request based on its header fields or log only failed requests. On the contrary, intercept-
ors deal with message bodies, for example, you can sign or compress the messages. Inter-
ceptors also come in two flavors: one for reading (they are executed when a message is
translated into a POJO, for example JSON to SeatDto ) and one for writing (they are
used for POJO to message translation).
We can add a simple server-side logging filter to our application by creating the following
package com.packtpub.wflydevelopment.chapter7.controller;
import java.util.logging.Logger;
import javax.inject.Inject;
public class LoggingRestFilter implements
ContainerRequestFilter, ContainerResponseFilter {
private Logger logger;
public void filter(ContainerRequestContext
requestContext, ContainerResponseContext responseContext)
throws IOException {
Search WWH ::

Custom Search