Equivalent of Servlet Filter for Jersey / JAX-RS / REST resources?

10,040

While not identical to servlet filters, Jersey supports ContainerResponseFilter and ContainerRequestFilter. These get called for all requests, so you have to do any URL matching in code.

The ResourceFilter was removed in Jersey 2.

Share:
10,040
necromancer
Author by

necromancer

Updated on June 29, 2022

Comments

  • necromancer
    necromancer almost 2 years

    In a regular Web Application, I can assign a chain of Filters to various paths for aspects such as Authentication, Authorization, Errors, Logging and more.

    The advantage is that I write servlets to focus on core functionality without worrying about infrastructure aspects. I can write orthogonal, cross-cutting Filters to authenticate, authorize, etc. Then I can weave them in web.xml. Looking at web.xml is enough to assure me that there are no holes in my application.

    Is this possible in JAX-RS or Jersey? If not, what is my best bet?

  • necromancer
    necromancer over 12 years
    i tried to edit your answer for completeness but pedants did not allow me to. could you please edit your answer to also include a mentino to ResourceFilter which is a better way to filter because it is on a per-resource basis. i am accepting your answer in the hope that you will add this information too. thanks :)
  • Scott Markwell
    Scott Markwell over 8 years
    The above link is broken