4

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
idmean
  • 14,540
  • 9
  • 54
  • 83
jjmartinez
  • 779
  • 5
  • 20
  • 37
  • 1
    Are you sure you have understood ClassNotFoundExceptions? I'm asking because you had a similar question lately... http://stackoverflow.com/questions/20762707/resteasy-service-deploy-in-tomcat-7-0-47 – pogopaule Dec 26 '13 at 12:10
  • Hehehe, of course, my friend. I have the same problems that with Resteasy. – jjmartinez Dec 26 '13 at 12:11
  • And of course, I do the same: include libraries in web-inf/lib, etc – jjmartinez Dec 26 '13 at 12:12
  • Ok, then maybe you want to have a look at http://tomee.apache.org/apache-tomee.html. This is tomcat + javaee libs on top. Take a look particularily at the JAX-RS version. http://tomee.apache.org/downloads.html – pogopaule Dec 26 '13 at 12:14
  • Does it work under Mac?? Oh, I need to use Tomcat, because it's part of a project that is using Tomcat :( – jjmartinez Dec 26 '13 at 12:14
  • yes, its java, it runs on a mac. ok, then how do you package your war file? maven? ant? by hand? – pogopaule Dec 26 '13 at 12:21
  • By hand, with the Eclipse export tool. I export to war automatically – jjmartinez Dec 26 '13 at 12:22
  • Then pls extract the war file and tell me what is inside the wars Web-Inf lib folder. I assume you didn't package the war correctly – pogopaule Dec 26 '13 at 12:47
  • Only META-INF and WEB-INF folder – jjmartinez Dec 26 '13 at 13:10
  • ok, there you have your problem. Your libs have to be inside the WEB-INF/lib folder. Revisit your packageing steps. Somehow you miss to include the required libs. Make sure you understand what has to go inside a war if you package it manually, otherwise follow the answer of Nicholas Karl. Good luck. – pogopaule Dec 26 '13 at 23:25

4 Answers4

10

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!!

jjmartinez
  • 779
  • 5
  • 20
  • 37
  • 1
    Thanks for pointing out the jars in the `/ext` folder, I missed those at first. And of course the servlet mapping piece is quite important and not mentioned in the Jersey getting started guide either. – nerdherd Mar 04 '14 at 18:09
0

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.

NicholasKarl
  • 253
  • 3
  • 12
0

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.

Worked for me.

NicholasKarl
  • 253
  • 3
  • 12
  • No, before I put my question here, I did all these steps and it didn't work. I had to put the libraries in the way that I put in my own answare. – jjmartinez Dec 28 '13 at 23:59
-1

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

siddmuk2005
  • 178
  • 2
  • 11