我是Web服务新手并尝试配置简单的REST服务。我正在关注教程http://www.vogella.com/tutorials/REST/article.html
我的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";
}
}
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>
我在点击网址时收到404
http://localhost:8080/ServletTest
http://localhost:8080/ServletTest/test
http://localhost:8080/ServletTest/test/hello
这里缺少什么,我没有得到它。
我能理解这一点,Jersey以某种方式提供了自己的Servlet实现,因此我不需要扩展HttpServlet。但是这个类应该被调整,因为教程说<param-value>
说服务所在的包
修改
部署了应用程序,我通过将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>");
}
}
现在,点击网址
localhost:8080/ServletTest/TestServlet
给我预期的结果
由于
答案 0 :(得分:0)