1

我在 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 时,它显示“请求的资源 () 不可用”。

我调用的 URL 是:http://localhost:8080/Calculator/hello/base

为什么我的网络服务没有被调用?

4

1 回答 1

0

正如 VGR 在评论中指出的那样,我将 JAX-RS 与 Servlet 混淆了。

我选择使用 servlet 路由。我删除了所有注释,并用重写了 HttpServlet 的 doGet 方法替换了我的 getHTML 方法。现在一切都按预期工作。

于 2018-11-29T17:57:56.437 回答