cyberbemon
cyberbemon

Reputation: 3200

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 

Upvotes: 7

Views: 16713

Answers (4)

abhinavroy23
abhinavroy23

Reputation: 3900

For me, there was a jar file added in Build path -> Libraries for which the actual jar was missing from the file system.

I removed the entry from build path and added the dependency in pom.xml. worked like a charm.

Upvotes: 0

cyberbemon
cyberbemon

Reputation: 3200

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1109865

That can happen if you haven't registered the JAX-RS servlet implementation in your web.xml at all. Jersey requires the following configuration:

<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>
</servlet>
<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>  

The com.sun.jersey.config.property.packages initialization parameter value must point to the package where all your services are. Your code snippet is however missing the package declaration. I'm not sure if this is omitted for brevity or not, but packageless classes are invisible to classes which are by itself in a package (such as the Tomcat and Jersey engines itself). The above web.xml example assumes that you've a

package com.example.service;

on your webservice classes. Fix or change it accordingly.

Note that the URL-pattern of /* thus means that ALL requests will be passed through Jersey. If you need to deploy other servlets, JSP or static content in the same webapp as well, you might want to specify a more specific URL pattern. E.g.

<url-pattern>/rest/*</url-pattern>

You'll only need to change your request URL to http://localhost/test_server/rest/hello.

Upvotes: 10

Oleksi
Oleksi

Reputation: 13097

Looks like you are registering your servlet in the wrong place. Double check that the root URL of your servlet, and make sure that matches what you are hitting.

Have you tried hitting?:

http://127.0.0.1/hello

Remember that /hello will go after whatever the base URL of your servlet is. Try looking a it in a debugger to see where it mounts.

Upvotes: 1

Related Questions