Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 231
cyberbemon Avatar asked May 09 '12 16:05

cyberbemon


2 Answers

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.

like image 182
BalusC Avatar answered Oct 26 '22 23:10

BalusC


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.

like image 30
Oleksi Avatar answered Oct 27 '22 01:10

Oleksi