0

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?

Jose Maria
  • 335
  • 1
  • 3
  • 12
  • Have you tried http://localhost:8080/rest/hola – morsor Jul 28 '15 at 11:24
  • Yes, but doesn't work – Jose Maria Jul 28 '15 at 11:35
  • The param-value in the web.xml should contain the java package name and not the project name. es.rest.test – Drona Jul 28 '15 at 11:35
  • Sorry, I changed the param-value to "es.rest.test" but doesn't work – Jose Maria Jul 28 '15 at 11:41
  • Are you getting HTTP 404 every time you say it doesn't work? – morsor Jul 28 '15 at 11:51
  • Secondly, the url-pattern in your servlet mapping should be as follows: /*. And the access URL should be http://localhost:8080/RestEjemplo/hola – Drona Jul 28 '15 at 12:00
  • @Drona I have changed the url-pattern but doesn't work. Eclipse shows me the same HTTP 404 message – Jose Maria Jul 28 '15 at 15:09
  • Yes @morsor , when Eclipse shows me the HTTP 404 message, I refer it like "doesn't work" because I should to see "Hola Jersey" – Jose Maria Jul 28 '15 at 15:10
  • I see all kinds of references to tutorials on other stackoverflow questions; at any point during the development of this test code, was the official jersey documentation referenced at all? https://jersey.java.net/documentation/latest/index.html and more specifically: https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3 – Gimby Jul 30 '15 at 10:52

2 Answers2

0

Jersey 2.0 does not recognize init-param with name com.sun.jersey.config.property.packages

Try to change it to

jersey.config.server.provider.packages

Then instead of :

http://localhost:8080/RestEjemplo/rest/hola

You can use something like this

 http://localhost:8080/es.rest.test/rest/hola

You may also want to look at this resourse Jersey REST Web Service, Tomcat, Eclipse and 404 error

Community
  • 1
  • 1
Rockstar
  • 2,228
  • 3
  • 20
  • 39
  • I get HTTP 404, again – Jose Maria Jul 28 '15 at 12:00
  • It seems you're using Jersey 2.0. check once with the updated answer. – Rockstar Jul 28 '15 at 12:02
  • I am using Jersey 2.19 and I change _com.sun.jersey.config.property.packages_ to _jersey.config.server.provider.packages_ and doesn't work. Also I was reading the thread you recommend me, and then I copy the code to a new project on Eclipse but doesn't work – Jose Maria Jul 28 '15 at 14:58
0

If your URL param is ,

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

hit,

http://localhost:8080/RestEjemplo/rest/hola/ok

Try This: Change your method and hit above url:

    @GET
    @Path("/ok")
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hola Jersey";
    }
VedantK
  • 9,728
  • 7
  • 66
  • 71