我正在尝试设置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.
这是我的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>
在src下我有一个名为“com.groupappserver.api”的包,它包含我的Status
类。这是代码:
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();
}
}
这是我的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>
为什么我收到此错误的任何想法?我该如何解决这个问题?
答案 0 :(得分:1)
你的web.xml中的Jersey servlet配置错误。
此servlet参数应包含JAX-RS资源类包的有效包名:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>GroupAppServer</param-value>
</init-param>
在您的情况下,包名称应为com.groupappserver.api
,而不是您指定的值GroupAppServer
。
答案 1 :(得分:1)
此外,您可以使用extends javax.ws.rs.core.Application 添加一个新类,并覆盖方法 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;
}
}