我已经在eclipse中创建了一个动态Web项目,并将资源文件定义为:
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; // Plain old Java Object it does not extend as class or implements // an interface // The class registers its methods for the HTTP GET request using the @GET annotation. // Using the @Produces annotation,it defines that it can deliver several MIME types,// text,XML and HTML. // The browser requests per default the HTML MIME type. //Sets the path to base URL + /hello @Path("/hello") public class Hello { // This method is called if TEXT_PLAIN is request @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { return "Hello Jersey"; } // This method is called if XML is request @GET @Produces(MediaType.TEXT_XML) public String sayXMLHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>"; } // This method is called if HTML is request @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello() { return "<html> " + "<title>" + "Hello Jersey" + "</title>" + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; } }
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>de.vogella.jersey.first</display-name> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>de.vogella.jersey.first</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
以下jar文件已经添加到web-inf中的lib文件夹中,并且在部署war文件之后也在web应用程序中进入目录结构.
一旦我启动tomcat和war文件,就会出现以下错误显示的问题:
org.apache.catalina.LifecycleException: Failed to start component
[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/de.vogella.jersey.first]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1074)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1858)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724) Caused by: java.lang.NoClassDefFoundError:
jersey/repackaged/com/google/common/base/Function
at org.glassfish.jersey.internal.ServiceFinder.(ServiceFinder.java:165)
at org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory.getAllServletContainerProviders(ServletContainerProviderFactory.java:66)
at org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer.onStartup(JerseyServletContainerInitializer.java:132)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5444)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
… 11 more Caused by: java.lang.ClassNotFoundException: jersey.repackaged.com.google.common.base.Function
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
… 16 more
**/de.vogella.jersey.first/WebContent/WEB-INF/lib/guava-16.0.1.jar /de.vogella.jersey.first/WebContent/WEB-INF/lib/jersey-client.jar /de.vogella.jersey.first/WebContent/WEB-INF/lib/jersey-common.jar /de.vogella.jersey.first/WebContent/WEB-INF/lib/jersey-container-servlet-core.jar /de.vogella.jersey.first/WebContent/WEB-INF/lib/jersey-container-servlet.jar /de.vogella.jersey.first/WebContent/WEB-INF/lib/jersey-server.jar**
但是这个错误还在,请告诉我我在哪里错了.
解决方法
hk2-api-2.2.0.jar hk2-locator-2.2.0.jar hk2-utils-2.2.0.jar javassist-3.18.1-GA.jar javax.annotation-api-1.2.jar javax.inject-2.2.0.jar javax.ws.rs-api-2.0.jar jersey-client.jar jersey-common.jar jersey-container-servlet.jar jersey-container-servlet-core.jar jersey-guava-2.8.jar jersey-server.jar,validation-api-1.1.0.Final.jar
希望有人可能会发现这有用.以上是已知的使用球衣版本2.8