vlio20
vlio20

Reputation: 9305

Jersey Maven configuration

I am trying to setup a Jersey RESTful service, the problem is that I am getting the following error when trying to do GET request to the following API: http://localhost:8080/GroupAppServer/api/status:

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

Here is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>GroupAppServer</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>jersey-servlet</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>GroupAppServer</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

under src I have a package called "com.groupappserver.api" which holds my Status class. Here is the code:

package com.groupappserver.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.groupservice.utils.Response;

@Path("/status")
public class Status
{
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getStatus() 
    {
        return new Response();
    }

}

Here is my 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>GroupAppServer</groupId>
    <artifactId>GroupAppServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>

    </dependencies>
</project>

Any ideas why I am getting this error? and how can I fix this?

Upvotes: 1

Views: 1014

Answers (2)

mgregori
mgregori

Reputation: 191

Also, you can add a new class with extends javax.ws.rs.core.Application and overwrite the method getClasses.

Put your com.groupappserver.api.Status class into the Set returned by getClasses :

package com.groupappserver.api;

@ApplicationPath("/*")
public class ApplicationConfig extends javax.ws.rs.core.Application {

    private final Set<Class<?>> classes;

    public ApplicationConfig() {
        final Set<Class<?>> _classes = new HashSet<>();
        _classes.add(Status.class);
        classes = Collections.unmodifiableSet(_classes);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }
}

Upvotes: 1

Konstantin V. Salikhov
Konstantin V. Salikhov

Reputation: 4653

Jersey servlet in your web.xml is misconfigured.

This servlet parameter should contain valid package name of your JAX-RS resource classes package:

   <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>GroupAppServer</param-value>
    </init-param>

In your case the package name should be com.groupappserver.api and not the value GroupAppServer you've specified.

Upvotes: 1

Related Questions