[JAX-RS] Use @Provider to leverage JAX-RS provider auto-discovery feature.

There are at least 2 ways to register a JAX-RS resource such as a set of web methods or the filters.

1. The first and the verbose way is to override the getClasses method in a subclass of Application. Like this:

The drawback with this approach is that you have to explicitly add all your resources here and this class may get quite big.

2. The second approach, a simpler one is to still extend the Application class, but this time you leave it empty. This way, auto-discovery kicks in. It will work for the classes containing @Path, however, how do you register filters? Simple! Just use the @Provider annotation on your filters. The application subclass becomes:

And don’t forget to add @Provider in on all your resource classes (Filters, Interceptors, Resources…)

Leave a Reply

Your email address will not be published. Required fields are marked *