[英]Getting started with Java RESTful service using javax, and Jersey Servlet dispatcher
I am new to web services and trying to configure a simple REST service. 我是Web服务的新手,正在尝试配置简单的REST服务。 I am following the tutorial http://www.vogella.com/tutorials/REST/article.html
我正在关注教程http://www.vogella.com/tutorials/REST/article.html
My TestClass is 我的TestClass是
package com.test.servlettest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class TestRestService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello Jersey";
}
}
And web.xml is 和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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>ServletTest</display-name>
<servlet>
<servlet-name>TestRestService</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>com.test.servlettest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestRestService</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>
I am getting 404 when i am hitting URLs 我到达网址时得到404
http://localhost:8080/ServletTest
http://localhost:8080/ServletTest/test
http://localhost:8080/ServletTest/test/hello
What is missing here, i am not getting it. 这里缺少什么,我不明白。
I could understand this, Jersey provides its own Servlet implementation somehow, so i do not not need to extend HttpServlet. 我可以理解,Jersey以某种方式提供了自己的Servlet实现,因此我不需要扩展HttpServlet。 But this class should be regietered as the tutorial says
<param-value>
says the package where services are there 但是应该重用此类,因为本教程说
<param-value>
说那里的服务包
EDIT 编辑
The application is deployed, i verified it by adding a servlet class to the same package 部署了应用程序,我通过将Servlet类添加到同一包中来对其进行了验证
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>Hello World</body></html>");
}
}
And now, hitting the URL, 现在,点击URL,
localhost:8080/ServletTest/TestServlet
is giving me the expected result 给了我预期的结果
Thanks 谢谢
Initially i was using a jar downloaded from 最初我使用的是从下载的jar
Java2s link for downloading the jar Java2s链接,用于下载jar
I replaced it with jersey bundle 1.17
and asm 3.3.1
. 我用
jersey bundle 1.17
和asm 3.3.1
代替了它。 It worked then 当时有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.