Rishi Prakash
Rishi Prakash

Reputation: 1789

The ResourceConfig instance does not contain any root resource classes exception in REST API

I am trying to run a REST Api built in java by me; but I am getting ResourceConfig instance does not contain any root resource exception, I know that this exception usually comes when the package name we gave as provider package does not contain any class with @Path annotation, but that's not the case here. Could you please look at web.xml and class and suggest what I need to change. I have tried to solve it honestly by myself ( for last 2 days)

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
   <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>restWebservice</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Java file

package restWebservice;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("Movies")
class RestController{

    @GET
    @Path("/{movieName}")
    public Movie getMovieDetails(@PathParam("{movieName}") String nameOfMovie){
        Movie movie=null;
        if(nameOfMovie.contains("Ragnarok")){
                new Movie("Thor-Ragnarok","Released on 3 November 2017 in India");
        }else{
            new Movie("Thor","Released on 29 April 2011 in India");
        }
        return movie;
    }

    @POST
    public Response saveMovieDetails(@QueryParam("movieName") String nameOfMovie, @QueryParam("releaseDate") String dateOfRelease){
        String finalSave = nameOfMovie + " and it's release Information "+ dateOfRelease;

        return Response.status(200).entity(finalSave).build();
    }

}


class Movie{

private String movieName;
private String movieReleaseDate;

public Movie(String name,String date){
this.movieName = name;
this.movieReleaseDate = date;

}


public void displayMovieInfo(){
System.out.println( this.movieName+" and "+ this.movieReleaseDate);
}


}


.

Tree view of Project

├── java
│   └── restWebservice
│       └── RestController.java
├── pom.xml
├── src
│   └── main
│       ├── resources
│       └── webapp
│           ├── WEB-INF
│           │   └── web.xml
│           └── index.jsp
└── target
    ├── classes
    │   └── restWebservice
    │       ├── Movie.class
    │       └── RestController.class
    ├── m2e-wtp
    │   └── web-resources
    │       └── META-INF
    │           ├── MANIFEST.MF
    │           └── maven
    │               └── RESTJava
    │                   └── MovieService
    │                       ├── pom.properties
    │                       └── pom.xml
    └── test-classes

Upvotes: 0

Views: 81

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209012

<param-name>jersey.config.server.provider.packages</param-name>

This param (which tells Jersey which package to scan) is for Jersey 2.x. The 1.x version is

com.sun.jersey.config.property.packages

Additionally:

  • Make your resource class public, move the model into its own class file, and make it public.
  • Your Java code should be in src/main/java so move the restWebservice folder into there

Upvotes: 1

Related Questions