Giuseppedes
Giuseppedes

Reputation: 137

Jersey tomcat and eclipse: error 404

I was following vogella "REST with Java (JAX-RS) using Jersey - Tutorial".

I created a new dynamic web project named "project" generating web.xml file.

I added all jar files from jaxrs-ri-2.23.1 in "eclipseWorkspace/project/WebContent/WEB-INF/lib"

I created a new class named Hello and copied the content from vogella tutorial:

package project;

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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

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

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

} 

I modified web.xml in this way:

<?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" 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>project</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>project</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Right click on project name, run as, Run on server. Choosed the existing tomcat v7 server at localhost.

Restarted server.

When i go to:

http://localhost:8080/project/rest/hello

It shows 404 error:

HTTP Status 404 - Not Found

type Status report

message Not Found

description The requested resource is not available.

Apache Tomcat/7.0.70

Do you know how to solve this?

Upvotes: 1

Views: 1442

Answers (1)

Giuseppedes
Giuseppedes

Reputation: 137

Solved unselecting the option "Build Automatically" in eclipse.

Then "Build project" and "Run on server" and it works.

Upvotes: 1

Related Questions