我在Eclipse中開發了一個使用Apache Tomcat的RESTful Web服務示例,但是我沒有完成這個示例。使用Apache Tomcat在Eclipse中運行RESTful Web服務
首先,我配置的Apache Tomcat上8080端口,所以當我運行服務器,我可以看到的歡迎屏幕。
我創建了一個名爲「RestEjemplo」項目,然後創建「Hola.java」類,這是在「es.rest.test」包
下面我展示的代碼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>";
}
}
而且這是我的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" 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>
在那之後,我檢查了PARAM值是我的項目的包的名稱相同,其他參數
我點擊了我的類Helo.java和「運行方式>在服務器上運行「和Eclipse自動啓動以下方向
http://localhost:8080/RestEjemplo/WEB-INF/classes/es/rest/test/Hola.java
哪,我認爲這是錯誤的。無論如何,Web瀏覽器都有錯誤。
Estado HTTP 404 -
type: Informe de estado
mensaje:
descripción: El recurso requerido no está disponible
Apache Tomcat/7.0.63
另外,如果我改變德URI/URL到另一個,有更多的意義上說,是這樣的:
http://localhost:8080/RestEjemplo/rest/hola
的Eclipse告訴我同樣的錯誤。 我不知道如何解決這個,因爲我認爲Apache的配置是好的,最後使用的URI也是可以的。 我錯了什麼?
你試過http:// localhost:8080/rest/hola – morsor
是,但不起作用 –
web.xml中的參數值應該包含java包名稱而不是項目名稱。 es.rest.test param-value> –
Drona