当我尝试使用Jersey和maven在Apache Tomcat上实现的HelloWorld RESTful Web服务时,我得到以下异常.
URL: http://localhost:8080/TestRest/rest/hello/abcd
com.sun.jersey.api.container.ContainerException:ResourceConfig实例不包含任何根资源类.
我看了互联网上的各种来源,他们说出现异常是因为web.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/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>
这是简单的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(); } }
最后,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>
我希望我的目录结构是正确的,请不要纠正我.