使用 Tomcat 和 Eclipse 的 Jersey 服务

2022-01-21 00:00:00 java eclipse jersey tomcat7 jersey-2.0

我正在使用 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 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.

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

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

这是我的 web.xml:

This is my 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>

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

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

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

推荐答案

我的解决方案:

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

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

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

这是我的 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>

com.myservice.services 是我的服务所在的包

感谢您的意见!!

相关文章