Java jersey 2.7 在 apache tomcat 7.0 上运行时出现问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22754372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jersey 2.7 issue while running it on apache tomcat 7.0
提问by Timothy Drisdelle
I am creating a jersey application using apache tomcat 7.0 and eclipse. I have created a dynamic web project in eclipse and have defined a resource file as:-
我正在使用 apache tomcat 7.0 和 eclipse 创建一个球衣应用程序。我在 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 file is also created as below-
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>
Following jar files have been added to lib folder in web-inf and are also coming in directory structure in web-apps after war file is deployed.
以下 jar 文件已添加到 web-inf 中的 lib 文件夹中,并且在部署 war 文件后也进入 web-apps 的目录结构中。
The problem which is coming as soon as i start tomcat and war file is deployed following error is displayed:
当我启动 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
org.apache.catalina.LifecycleException:无法在 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) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1074) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1858) ) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 在 java。util.concurrent.FutureTask.run(FutureTask.java:166) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)在 java.lang.Thread.run(Thread.java:724) 引起:java.lang.NoClassDefFoundError: jersey/repackaged/com/google/common/base/Function at 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) 在 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 at org .apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) 在 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ... 16 更多
I have added following jars to lib folder:
我已将以下 jars 添加到 lib 文件夹中:
**/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**
But still this error is coming, please tell me where i am going wrong.
但是仍然会出现此错误,请告诉我哪里出错了。
回答by Juned Ahsan
I believe you are missing Google guava dependency jars missing. Try to download and add them to your classpath.
我相信你错过了谷歌番石榴依赖罐。尝试下载它们并将它们添加到您的类路径中。
回答by Akalanka
At last solved. Please check below jar files. It is disgusting to use this number of jar files to implement REST services. May be restlet or easy rest is better than jersey.
终于解决了。请检查以下 jar 文件。用这么多的jar文件来实现REST服务,真是恶心。可能是restlet或easy 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
Hope someone may find this useful. The above is known working with jersey version 2.8
希望有人会发现这很有用。以上是已知的球衣版本 2.8
回答by Eric Rajkovic
jersey-guava-2.x.jar is the jar you may be missing on your runtime setup - you may want to package all the extension jars as found under jaxrs-ri/ext from the Jersey JAX-RS 2.0 RI bundle - see https://jersey.java.net/download.html
jersey-guava-2.x.jar 是您在运行时设置中可能缺少的 jar - 您可能想要打包在 Jersey JAX-RS 2.0 RI 包中的 jaxrs-ri/ext 下找到的所有扩展 jar - 请参阅https ://jersey.java.net/download.html
回答by Sri Confucious
Was able to get the basic REST webservice working for Jersey 2.8.Do let me know if there is a better way to do this.
能够获得适用于 Jersey 2.8 的基本 REST web 服务。请告诉我是否有更好的方法来做到这一点。
pom.xml:
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:
网页.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:
问候.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!!";
}
}
回答by YaBoiSandeep
You need to add the following jar files to your web-inf/lib folder, also you need to configure build path -- Right click your project--Build path -- configure build path -- libraries -- add external jars (add all the below jar files). And restart server
您需要将以下jar文件添加到您的web-inf/lib文件夹中,您还需要配置构建路径--右键单击您的项目--构建路径--配置构建路径--库--添加外部jar(添加所有下面的 jar 文件)。并重启服务器