Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes

I am getting the following exception when I try to hit a HelloWorld RESTful web service implemented using Jersey and maven on Apache Tomcat.

URL: http://localhost:8080/TestRest/rest/hello/abcd

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

I looked at various sources on internet, they say the exception occurs because of not having class in the package structure given in web.xml, but I made sure that they are all correct.

Any help regarding this is greatly appreciated.

Following is the code of pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>my.test.rest</groupId>
      <artifactId>TestRest</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>TestRest Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <repositories>
                <repository>
                <id>maven2-repository.java.net</id>
                <name>Java.net Repository for Maven</name>
                <url>http://download.java.net/maven/2/</url>
                <layout>default</layout>
        </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.8</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>TestRest</finalName>
  </build>
</project>

This is the simple HelloWorldService.

package com.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Hello, " + msg;

        return Response.status(200).entity(output).build();

    }

}

And finally, the web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>com.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

I hope the directory structure of mine is correct too, please correct me if it isn't.

Directory Structure

like image 784
kausal_malladi Avatar asked Nov 08 '12 16:11

kausal_malladi


1 Answers

You have the HelloWorldService.java in the wrong source folder. It must be in src/main/java not in src/main/resources. It's a big big difference!

It means that your Java code has not been compiled and that's why you get the error. No class that can handle the request.

Take a look at the Maven Standard Directory Layout.

like image 161
maba Avatar answered Oct 16 '22 22:10

maba