I have a class, Services, that extends javax.ws.rs.core.Application
as shown below. It overrides getSingletons()
to ensure that there is only a single instance of the resource class, CommonServices.
@ApplicationPath("/")
public class Services extends Application {
@Override
public Set<Object> getSingletons() {
Set<Object> resources = new HashSet<>();
set.add(new CommonServices());
return set;
}
}
In my resource class:
@Path("/")
public class CommonServices{
public CommonServices() {
}
...
}
I deployed the application to WebLogic, which uses Jersey implementations of JAX-RS. After starting the application, I noticed that two instances of CommonServices were instantiated instead of just one (I put debug statements inside its constructor). These instantiations happened before any service calls were made. Why are two instances of CommonServices instantiated?
Below is a snippet of my web.xml showing the relevant part for JAX-RS. I have to comment out the servlet-mapping part. If I don't comment out, during deployment, WebLogic will complain that the url-pattern /* in the web application is mapped to multiple servlets. Why?
<servlet>
<servlet-name>JAX-RS 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.xxx.xxx.xxx</param-value>
</init-param>
<init-param>
<param-name>wl-dispatch-policy</param-name>
<param-value>HighPriorityWorkManager</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>-->
EDIT
I found that the issue is due to getSingletons() and getClasses() being called twice by the container. To resolve the issue, I just need to add the resources to the set in the constructor instead of in getSingletons(). This way, only one instance of CommonServices is ever called.