Java Jersey 服务与 Tomcat 和 Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20784531/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:18:53  来源:igfitidea点击:

Jersey Services with Tomcat and Eclipse

javaeclipsejerseytomcat7jersey-2.0

提问by jjmartinez

I'm developing a rest service with Jersey 2.0 (I downloaded from http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.5/jaxrs-ri-2.5.zip) and I'm using Tomcat 7.0.47. I run Tomcat from Eclipse and my machine is a Mac.

我正在使用 Jersey 2.0 开发休息服务(我从http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.5/jaxrs-ri-2.5.zip下载)并且我正在使用 Tomcat 7.0.47。我从 Eclipse 运行 Tomcat,我的机器是 Mac。

I'm using the Eclipse tool to generate a WAR and to deploy the service.

我正在使用 Eclipse 工具生成 WAR 并部署服务。

This is my web.xml:

这是我的 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>MyServices</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>com.service.services.Services</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

I include all Jersey jars into WEB-INF/lib, except javax.servlet-api-3.0.1.jarthat it is into Apache/lib When I deploy in Tomcat, it shows a very strange error caused by:

我把所有的Jersey jars都包含到WEB-INF/lib中,除了javax.servlet-api-3.0.1.jar它是到Apache/lib中,当我在Tomcat中部署时,出现了一个很奇怪的错误,原因是:

Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.ClientConfig
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    ... 70 more

采纳答案by jjmartinez

My solution:

我的解决方案:

  1. Add into Tomcat/lib all libraries that you download from Jersey and are including into /extfolder of the Jersey .zip
  2. Add into Web-Inf/lib of my project only libraries that are under /libfolder of the Jersey zip file
  3. Add into Web-Inf/lib of my project javax.ws.rs-api-2.jarthat you can find in /apifolder of Jersey
  1. 将您从 Jersey 下载并包含/ext在 Jersey .zip 文件夹中的所有库添加到 Tomcat/lib
  2. /lib将位于 Jersey zip 文件文件夹下的库添加到我的项目的 Web-Inf/lib 中
  3. 添加到我的项目的 Web-Inf/lib 中javax.ws.rs-api-2.jar,您可以在/apiJersey 的文件夹中找到

With this, I don't have problems to run Tomcat with Jersey.

有了这个,我在 Jersey 上运行 Tomcat 就没有问题了。

And this is my web.xml for Jersey 2.0

这是我的 web.xml Jersey 2.0

<?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>MyRESTServices</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 Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>com.myservice.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

Where com.myservice.servicesis the package where I have my services

哪里com.myservice.services是我有我的服务包

Thanks for your comments!!

感谢您的意见!!

回答by NicholasKarl

It works for me on a PC. Did you make the eclipse project a maven project? If you do that then you can use org.glassfish.jersey.bundles jaxrs-ri version 2.5 (or whichever) as a dependency in your pom and then when you do run->maven-install from eclipse it will build a war file with all the necessary dependencies in it.

它在 PC 上对我有用。你把eclipse项目做成maven项目了吗?如果你这样做,那么你可以使用 org.glassfish.jersey.bundles jaxrs-ri 2.5 版(或任何一个)作为你的 pom 中的依赖项,然后当你从 eclipse 运行 -> maven-install 时,它会构建一个 war 文件其中包含所有必要的依赖项。

回答by NicholasKarl

Adding Jersey to an Eclipse project without maven is simple: Click properties for the project, go to the Java Build Path. Click on the Add Library on the left, select user library. After you click next you will see "user libraries" in the upper right. Click that. Click new. Add a name: jaxrs-ri-2.5 for example. Click add external jars, and add the jars in the api, lib and ext folders. You're done. This will probably also include them in any war file you make with eclipse.

在没有 maven 的情况下将 Jersey 添加到 Eclipse 项目很简单:单击项目的属性,转到 Java Build Path。单击左侧的添加库,选择用户库。单击下一步后,您将在右上角看到“用户库”。单击那个。点击新建。添加一个名称:例如 jaxrs-ri-2.5。点击添加外部jar,在api、lib、ext文件夹中添加jar。你完成了。这可能还会将它们包含在您使用 eclipse 制作的任何 war 文件中。

Worked for me.

为我工作。

回答by siddmuk2005

Go with jdk1.7.0_04 and install apache-tomcat-7.0.55 and set the environment variable for this setting and set eclipse for same for compiling building and runtime. then everything will be OK for Jax-RS-2.1

使用 jdk1.7.0_04 并安装 apache-tomcat-7.0.55 并为此设置设置环境变量,并将 eclipse 设置为相同以编译构建和运行时。那么对于 Jax-RS-2.1 来说一切都会好起来的