users@jersey.java.net

[Jersey] Re: Adding extra resource class to application

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 12 Sep 2014 22:13:21 +0200

On 09 Sep 2014, at 02:19, Dunstan, Tom (EPA) <Tom.Dunstan_at_epa.sa.gov.au> wrote:

> Hi all
>
> We currently have an application that uses class scanning and annotations to pick up all of our resources. We just have an empty application class that extends Application and it seems that Jersey picks up our resource classes and a number of other packaged components such as JPA exception mappers.
>
> I’d like to register the EncodingFilter and GZipEncoder so that our content gets compressed, but if I return those classes from an overridden getClasses() method on the application, none of the other components are registered automatically anymore. I can manually add in the ones from my application (though that’s not very user-friendly) but I have no idea how to get a list of all the other parts that it is picking up.

It is not advised to rely on classpath scanning for real projects. It may be OK for prototypes or demos, but in production deployments, it is dangerous, because, as you have noted - you never know what ends up in your application.

You can try to get the list of all the providers picked up by classpath scanning by enabling monitoring and then checking the Jersey application MBeans using e.g. jconsole or visual vm (see the "YourApp / Global / Configuration" section for the list of registered providers.

Then you can inherit your JAX-RS Application from ResourceConfig to list all the packages that should be scanned. It shouldn't be inconvenient at all. See e.g. this example.

>
> Is there a way to just add some components like filters / encoders to the default set?

No, that is not possible.
>
> Also, is it possible to only enable the gzip filter for specific resources? I’m hitting this issue when it’s applied to our JSONP resources:https://java.net/jira/browse/JERSEY-2524.

You would need to create a custom name-binding annotation, .e.g. @GZippable.
Then you would need to create a new class (e.g. NameBoundGZipEncoder) that inherits from the Jersey GZipEncoder class and has the @GZippable annotation declared.
Then you would register your NameBoundGZipEncoder in your application. This encoder would then only be applied to those resource methods that would also be annotated with @GZippable.

We should probably add support for this directly into Jersey at some point - care to file an enhancement request in our Jira?

> (We’re using Jersey 2.0 embedded in Glassfish 4.0.)

Hmm... that version is more than 15 months old now :(
...but I have heard that there could be a new release GF just behind the door soon ;-)

Cheers,
Marek

>
> Thanks
>
> Tom