简体   繁体   English

泽西岛Maven配置

[英]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 : 我正在尝试设置Jersey RESTful服务,问题是尝试对以下API进行GET请求时遇到以下错误: 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: 这是我的web.xml文件:

<?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. 在src下,我有一个名为“ com.groupappserver.api”的程序包,其中包含我的Status类。 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: 这是我的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? 我该如何解决?

Jersey servlet in your web.xml is misconfigured. 您的web.xml中的Jersey servlet配置错误。

This servlet parameter should contain valid package name of your JAX-RS resource classes package: 此servlet参数应包含您的JAX-RS资源类软件包的有效软件包名称:

   <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. 在您的情况下,程序包名称应为com.groupappserver.api而不是您指定的值GroupAppServer

Also, you can add a new class with extends javax.ws.rs.core.Application and overwrite the method getClasses . 另外,您可以添加具有扩展javax.ws.rs.core.Application的新类,并覆盖方法getClasses

Put your com.groupappserver.api.Status class into the Set returned by getClasses : 将您的com.groupappserver.api.Status类放入getClasses返回的Set中:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM