I was developing a RESTful Web Service's example using Apache Tomcat in Eclipse, but I don't accomplish this example works.
First,I configured Apache Tomcat on 8080 port, so when I run the server I can see the welcoming screen.
I created a project called "RestEjemplo", then I created "Hola.java" class which is in "es.rest.test" package
Below I show the code of Hola.java
package es.rest.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// POJO, no interface no extends
// Sets the path to base URL + /hola
@Path("/hola")
public class Hola {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hola Jersey";
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>" + "<hola> Hola Jersey" + "</hola>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html> " + "<title>" + "Hola Jersey" + "</title>" +
"<body><h1>" + "Hola Jersey" + "</h1></body>" +
"</html>";
}
}
And it is my web.xml file
<?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>RestEjemplo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>es.rest.test</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>
After that, I checked that param-value was the same name of my project's package, and the others parameters
I click over my class Helo.java and "Run as > Run on Server" and Eclipse launch the following direction automatically
http://localhost:8080/RestEjemplo/WEB-INF/classes/es/rest/test/Hola.java
which, I think that it is wrong. In any case, the web browser has an error.
Estado HTTP 404 -
type: Informe de estado
mensaje:
descripción: El recurso requerido no está disponible
Apache Tomcat/7.0.63
In addition, if I change de URI/URL to another that has more sense, like this:
http://localhost:8080/RestEjemplo/rest/hola
Eclipse show me the same error. I don't know how to solve this because I think that Apache's configuration is OK, and the last URI used is also OK. What is it that I'm wrong?