我正在使用apache tomcat 7.0和eclipse创建一个jersey应用程序。 我在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-apps中进入目录结构。
一旦我启动tomcat和war文件,就会出现以下错误后出现的问题:
org.apache.catalina.LifecycleException:无法启动组件 [StandardEngine [卡塔利娜] .StandardHost [本地主机] .StandardContext [/de.vogella.jersey.first]] 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) 在org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 在org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 在org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634) 在org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1074) 在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) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:724)引起:java.lang.NoClassDefFoundError: 球衣/重新打包/ COM /谷歌/普通/碱/功能 在org.glassfish.jersey.internal.ServiceFinder。(ServiceFinder.java:165) 在org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory.getAllServletContainerProviders(ServletContainerProviderFactory.java:66) 在org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer.onStartup(JerseyServletContainerInitializer.java:132) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5444) 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 11更多引起:java.lang.ClassNotFoundException:jersey.repackaged.com.google.common.base.Function 在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) 在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ......还有16个
我在lib文件夹中添加了以下jar:
**/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**
但是这个错误仍然存在,请告诉我哪里出错了。
答案 0 :(得分:44)
终于解决了。请检查下面的jar文件。使用这么多jar文件来实现REST服务真是令人厌恶。可能是重新安排或轻松休息比运动衫更好。
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。
答案 1 :(得分:4)
我相信你遗失了谷歌番石榴依赖罐。尝试下载并将它们添加到类路径中。
答案 2 :(得分:1)
能否为Jersey 2.8提供基本的REST网络服务。如果有更好的方法,请告诉我。
的pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.8</version>
</dependency>
</dependencies>
的web.xml:
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.servlet.provider.webapp</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Greeting.java:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("greeting")
public class Greeting {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String greeting(){
return "Welcome!!";
}
}
答案 3 :(得分:1)
您需要将以下jar文件添加到您的web-inf / lib文件夹中,您还需要配置构建路径 - 右键单击您的项目 - 构建路径 - 配置构建路径 - 库 - 添加外部jar (添加以下所有jar文件)。并重启服务器
答案 4 :(得分:0)
jersey-guava-2.x.jar是您在运行时设置中可能缺少的jar - 您可能希望将来自Jersey JAX-RS 2.0 RI包的jaxrs-ri / ext下的所有扩展jar打包 - 见https://jersey.java.net/download.html