我在 Eclipse 上创建了一个 Web 项目,其中包含一个类:“HelloWorld.java”,它应该有一个响应 GET 请求的方法。
package javaeetutorial.hello;
// imports
@Path("base")
public class HelloWorld extends HttpServlet {
public HelloWorld() {
}
@GET
@Produces("text/html")
public String getHtml() {
return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
}
}
然后,在 WebContent 文件夹的 WEB-INF 目录中,我创建了一个包含以下内容的 web.xml 文件,以便将/hello url 的请求映射到我的 servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="true"
version="3.1">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>javaeetutorial.hello.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
我将项目导出到 .war 文件,然后使用 Glassfish 部署它,但是当我调用应该调用我的 Web 服务的 URL 时,它显示“请求的资源 () 不可用”。
我调用的网址是:http://localhost:8080/Calculator/hello/base
为什么我的网络服务没有被调用?
最佳答案
正如 VGR 在评论中指出的那样,我将 JAX-RS 与 Servlet 混淆了。
我选择了 servlet 路线。我删除了所有注释,并用 HttpServlet 的 doGet 方法的重写替换了 getHTML 方法。现在一切都按预期进行。
关于java - 为什么我不能调用这个简单的 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53544401/