Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Services with Tomcat and Eclipse

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.

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

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>

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
like image 368
jjmartinez Avatar asked Jan 11 '23 14:01

jjmartinez


1 Answers

My solution:

  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

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

And this is my web.xml for 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.services is the package where I have my services

Thanks for your comments!!

like image 106
jjmartinez Avatar answered Jan 21 '23 22:01

jjmartinez