Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jersey 2.7 issue while running it on apache tomcat 7.0

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

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-

<?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.

The problem which is coming as soon as i start tomcat and war file is deployed following error is displayed:

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

I have added following jars to lib folder:

**/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.

like image 321
Timothy Drisdelle Avatar asked Mar 31 '14 05:03

Timothy Drisdelle


1 Answers

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.

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

like image 173
Akalanka Avatar answered Nov 16 '22 01:11

Akalanka