cyberbemon cyberbemon - 4 months ago 26
Java Question

Jersey hello world gives 404

I have the following code in my java class

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {
//This method is called is TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
return "Hello World";
}

//this method is called if TEXT_XML is requested
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>";
}

//this method is called if HTML is requested
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>";
}
}


I compiled it and exported it as a .WAR file , when I type


http://127.0.0.1/test_server/hello


I get a 404 . I tried it in WTP, cURL they all return 404.. I'm using tomcat 7.0.26

Note: I am running Tomcat on port 80, and other servlets respond as expected.

web.xml config

<display-name>Jersey_Test</display-name>
<servlet>
<servlet-name>Hello</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>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/*</url-pattern>


The following URL gives me Http status 500

http://localhost/Jersey_Test/rest/hello
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

Answer

The problem has been fixed and this is how I did it.

I removed the jersey .jar files from the build path and replaced them in the WEB-INF\lib folder and everything worked.