Question

I am trying to create a hello world restful(jersey) webservice. I am using Spring tool suite and the native vFabric tcServer.

Following is the URL : /localhost:8080/example/rest/hello which gives 404.

I have ensured all the jars are present in the WEB-INF/lib folder in the IDE.

I read through various forums which mention that all the jars should be present in the WEB-INF directory. I have been through various SO questions on the same topic but I still am unable to resolve the cause for this error.

Do I need to specifically navigate to tcServer installation in workspace to add these jars? Any ideas?

I am stuck with the following error when I start the tcServer:

Jan 13, 2013 7:19:23 PM org.apache.catalina.core.ApplicationContext log

INFO: Marking servlet jersey as unavailable
Jan 13, 2013 7:19:23 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet  threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1136)

Following is the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>example</display-name>
  <servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.example.service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

and the corresponding webservice class:

package com.example.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "whats up";
    }

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHelloXmlVersion() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHelloHTMLVersion() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
    }
}

List of all jar files in the project:

asm-3.1.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
jersey-client-1.16.jar
jersey-core-1.16.jar
jersey-json-1.16.jar
jersey-server-1.16.jar
jersey-servlet-1.16.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
jsr311-api-1.1.1-javadoc.jar
Was it helpful?

Solution

Got it Fixed. Had to refer to the web project name in the web.xml:

  .....
 <display-name>SampleWebService</display-name>
  .......

And the corresponding url:

/localhost:8080/SampleWebService/rest/hello
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top