• REST
    March 3, 2015

When to Use JAX-RS Class-path Scanning Mechanism

The short answer is: never!. I have seen several nasty (so called) "bugs" caused by this feature so that i felt like sharing this little advice via blog post:

Never ever use JAX-RS class-path scanning feature in a production environment

Class-path scanning looks like a handy feature. Let say you do not want bother with enumerating all the components you would like to include in your JAX-RS application. Then your JAX-RS application class could look like:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
public class EasyToConfigureJaxRsApplication extends Application {

The above example works great. When started, the application just scans the actual class-path, and adds every single JAX-RS component class found there to the actual runtime configuration. Isn't is great? Frankly, this kind of configuration could work just fine. Until someone changes either the system configuration (system class-path) or the way how you application is being packaged (a new 3rd party component could be added/removed from the application class-path then). These changes could be out of your control and if one of them happens, you application configuration could break. For this reason, it is not wise to use this kind of configuration in a production environment.

One could think that for development purposes, using this cool feature would be fine. I think otherwise. You either develop an application targeted for production use, and then you want to stick with whatever will be used in production eventually (where class-path scanning is a no-go) or you just want to look around and play with things. In the latter case you still want to keep control and avoid any nasty surprise.

To end up in a positive way. Here is another advise: Should you need to use any kind of scanning (for any reason), check out Jersey package scanning feature that allows you to control the scope of scanning. See the following links for some more details: https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/ResourceConfig.html#packages(java.lang.String...), https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/ServerProperties.html#PROVIDER_PACKAGES

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.