简体   繁体   中英

Running a RESTful Web Service in Eclipse with Apache Tomcat

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?

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

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";
    }

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