简体   繁体   中英

404 when accessing Jersey app in Tomcat 7

I am a newby in web development and I am struggling to create a simple rest web service using jersey, packed as an independent war file, to be deployed on tomcat 7.

I have followed this tutorial in order to create simple hello world restful web service. I have used intelij in order to create the war file (both war file and exploded version of it) and deployed them on the tomcat\\webapps folder.

When starting up the tomcat, I fail to access the url ( http://localhost:8080/rest/hello ) - getting 404.

Looking at the tomcat's logs, I can't find any exception or error, other then these next set of lines:

INFO: Scanning for root resource and provider classes in the packages:
  sample
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class sample.Hello
εαΘ 30, 2013 11:24:38 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.

I have searched it a lot and tried to follow all solutions for this error but nothing worked.

I have the feeling that I missing something very basic, due to my newbi'ness. Any ideas what to search ? what am I missing ? it's a simple hello world app and I am exahuster and frustrated...

Java class

package sample;

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 Hello {

  // This method is called if TEXT_PLAIN is requested
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHelloInPlainText() {
    return "Hello world!";
  }

  // This method is called if HTML is requested
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHelloInHtml() {
    return "<html> " + "<title>" + "Hello world!" + "</title>"
        + "<body><h1>" + "Hello world!" + "</body></h1>" + "</html> ";
  }
}

web.xml

 <?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_3_0.xsd"
    id="WebApp_ID" version="3.0">

      <display-name>rest</display-name>

      <servlet>
        <servlet-name>Jersey REST Service</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>sample</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>

This is similar to this SO question: " No provider classes found: when running Jersey REST example application ".

The message "No provider classes found" is fine, no need to worry about it. For the URL http://localhost:8080/rest/hello to work your WAR file needs to be called rest.war . Then rest becomes the context root. All the paths you define in the project's annotations are relative to the context root .

if you have called your project in eclipse: rest, so you should get the correct message: "Hello world!" from tomcat with the url:

http://localhost:8080/rest/hello.

If you for example have called your project: MyHello, with the code you posted the correct url should be:

http://localhost:8080/MyHello/hello

(The reason is that Eclipse create by default a war called as the name of the project and publish it to the tomcat server you have added in your configuration)

Let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM