Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Jersey Filter?

Tags:

I want to know basically what a Jersey filter is and how is it related to a servlet filter? Are they the same? What are the main patterns of using a Jersey Filter?

like image 256
meisam Avatar asked May 03 '11 06:05

meisam


People also ask

What are Jersey filters?

A Jersey filter is not the same as a servlet filter. There are two filters included in Jersey, a filter for logging requests and one for compression (GZip). Another use case for a custom filter would be authentication or authorization.

What is ContainerResponseFilter?

public interface ContainerResponseFilter. An extension interface implemented by container response filters. By default, i.e. if no name binding is applied to the filter implementation class, the filter instance is applied globally to any outgoing response.

What is difference between filters and interceptors?

Filters and interceptors can be used on both sides, on the client and the server side. Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams.

What is Jersey servlet?

Jersey is a Java library for both serving and calling REST (or mainly HTTP, since not everything is REST) APIs. It's build on top of Java EE specifications, so it can be used on any server that implements these specifications, e.g. Tomcat. In your web. xml file you can define multiple servlets.


1 Answers

Technically, a Jersey filter is not a servlet filter. However, you can use a Jersey filter for many of the same things that you would use a servlet filter for - cross-cutting concerns that affect all (or some, or most) the services that Jersey exposes.

As the previous answer states, Jersey comes with two filters, but you can usefully implement the Jersey interfaces ContainerRequestFilter or/and ContainerResponseFilter if you don't want to extend them. You're not limited to these two.

Another Jersey interface to keep in mind is ResourceFilter - this interface can be implemented for a filter that affects only some of the services.

like image 91
Eyal Avatar answered Nov 17 '22 06:11

Eyal