[英]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.我正在 Eclipse 中使用 Apache Tomcat 开发 RESTful Web 服务的示例,但我没有完成此示例的工作。
First,I configured Apache Tomcat on 8080 port , so when I run the server I can see the welcoming screen.首先,我在8080 端口上配置了Apache Tomcat ,所以当我运行服务器时,我可以看到欢迎屏幕。
I created a project called " RestEjemplo ", then I created "Hola.java" class which is in "es.rest.test" package我创建了一个名为“ RestEjemplo ”的项目,然后我创建了“es.rest.test”包中的“Hola.java”类
Below I show the code of Hola.java下面我展示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这是我的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>
After that, I checked that param-value was the same name of my project's package, and the others parameters之后,我检查了 param-value 与我的项目包的名称相同,其他参数
I click over my class Helo.java and "Run as > Run on Server" and Eclipse launch the following direction automatically我点击我的类Helo.java和“运行方式 > 在服务器上运行”和 Eclipse 自动启动以下方向
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:此外,如果我将 de URI/URL 更改为另一个更有意义的,如下所示:
http://localhost:8080/RestEjemplo/rest/hola
Eclipse show me the same error. Eclipse 向我展示了同样的错误。 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.
不知道怎么解决这个问题,因为我觉得Apache的配置是可以的,最后使用的URI也是可以的。 What is it that I'm wrong?
什么是我错了?
Jersey 2.0 does not recognize init-param
with name com.sun.jersey.config.property.packages
Jersey 2.0 无法识别名称为
com.sun.jersey.config.property.packages
init-param
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您可能还想查看这个资源Jersey REST Web Service、Tomcat、Eclipse 和 404 错误
If your URL param is ,如果您的网址参数是 ,
<url-pattern>/rest/*</url-pattern>
hit,打,
http://localhost:8080/RestEjemplo/rest/hola/ok 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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.