user2287966
user2287966

Reputation: 265

Jersey 2.0 - Glassfish 4 Cast exception

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

Upvotes: 1

Views: 4890

Answers (1)

Anthony Accioly
Anthony Accioly

Reputation: 22471

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

Upvotes: 1

Related Questions