I am deploying JAX-RS web services to a Tomcat servlet container.
I have seen code examples that use either of the following two methods of indicating the resources in the web.xml
file:
method 1 - using the `jersey.config.server.provider.packages` init-param
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...where the resources are expected to reside in the com.example
package and I suppose are discovered by means of Java RTTI.
method 2 - using the `javax.ws.rs.Application` init-param
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>full.qualified.name.to.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
... where the MyApplication
class identifies explicitly the resource classes:
public class MyApplication extends javax.ws.rs.core.Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(ResourceA.class);
return s;
}
Is using the one versus the other method purely a matter of taste and configuration effort and what are some trade-offs to consider? Personally, I prefer the more fine-grained control offered by method 2, however the maven Jersey 2.7 archetype:
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
-DarchetypeVersion=2.7
... is using method 1 and that got me thinking.