Clientrequestfilter vs Containerrequestfilter

I knew that filters are used to process a request and can do something with the http and httpmethods header, but they confuse me with

What is the difference between clientrequestfilter and containerrequestfilter? In which scenario should we use clientrequestfilter and containerrequestfilter?

I tried from this site , but there were no details about it.

Please help me figure this out.

+7
java rest web-services jersey jax-rs
source share
1 answer

There are two sides to the REST interaction, client and server. Jersey / JAX -RS-2 has both a client API and a β€œcore” server API. When working with the client API, we could use ClientRequestFilter , and when using the server-side API, we would use ContainerRequestFilter . There is no way to mix and match them, they should be strictly used from the corresponding side of the interaction.

A ContainerRequestFilter (Server Side) example was supposed to do some authorization / authentication, which is a pretty common use case for a filter on the server side. The filter will be called before reaching any of your resources.

 Client ---> Internet ---> Server ---> Filter ---> Resource 

A ClientRequestFilter (client side) example will implement some cache client (like a mock browser cache). Or the case (which has already been implemented) is a filter for encoding the username and password for BASIC authentication. Before the request is actually sent to the server, a client filter will be called.

 Client ---> Filter ---> Internet ---> Server ---> Resource 

There are also XxxResponseFilters that follow the next thread

 Resource ---> ContainerResponseFilter ---> Server ---> Internet ---> Client Server ---> Internet ---> ClientResponseFilter ---> Client 
+10
source share

All Articles