1

I am trying to deploy a simple application that displays the status of a restful service using jersey and glassfish 4. I am getting the following exception:

org.glassfish.jersey.internal.ServiceConfigurationError: 
org.glassfish.jersey.internal.spi.AutoDiscoverable: The class 
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable implementing 
provider interface org.glassfish.jersey.internal.spi.AutoDiscoverable could not be 
instantiated: Cannot cast 
org.glassfish.jersey.server.internal.monitoring.MonitoringAutodiscoverable to 
org.glassfish.jersey.internal.spi.AutoDiscoverable

My web.xml looks like this

<web-app>
<servlet>
  <servlet-name>Servlet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>Servlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

My service looks like this:

@Path(MyServices.SERVICE_URL)
public class MyServices
{
   @GET
   @Produces("text/html")
   public String getStatus()
   {
      return "My service is running.";
   }
}

Does anyone knows what I am doing wrong here?

Edit I added this class to my war project:

@ApplicationPath("/*")
public class MyApplication extends ResourceConfig
{
   public MyApplication()
   {
      packages("com.java.services");
   }
}

and I am still getting the same exception:

javax.servlet.ServletException: Servlet.init() for servlet com.java.services.MyApplication threw exception
user2287966
  • 257
  • 1
  • 4
  • 10

1 Answers1

1

GlassFish 4.0 is a Servlet 3.x Container, so you have to change the deployment model

<!-- Servlet declaration can be omitted in which case
     it would be automatically added by Jersey -->
<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

But even this web.xml setting is no longer necessary, just implement a Application subclass annotated with @ApplicationPath and everything will work as expected

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("org.foo.rest;org.bar.rest");
    }
}

Result

Result

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • Anthony, please see my comment above. I am still getting the same exception. I tried with the web.xml, without the web.xml, with the web.xml and MyApplication, etc. – user2287966 Mar 06 '14 at 13:11
  • Well, I've just run your sample to be sure (no web.xml, and extending `Application` directly so that I do not have any dependencies to jersey on my project). Maybe you screwed your Class-path or something? (You need no dependencies on the project. If you are running your appplication with Maven all you need is `jax.ws.rs-api` as `provided`. [GlassFish already includes](https://jersey.java.net/documentation/latest/user-guide.html#servlet-app-glassfish) everything for you. – Anthony Accioly Mar 06 '14 at 17:26
  • I noticed that I was using jersey 2.6 and for some reason it was giving me problems. I deleted that jar from the classpath and used the one included in glassfish and everything worked as expected. Thanks for your help. – user2287966 Mar 11 '14 at 18:33
  • I'm glad to be of service. GlassFish 4 is dependent on Jersey 2.0, but if you really need newer versions there are scripts to update GlassFish 4.0.1. See http://blog.dejavu.sk/2014/01/21/updating-jersey-2-in-glassfish-4/ – Anthony Accioly Mar 11 '14 at 19:44