简体   繁体   English

为什么不能叫这个简单的网络服务?

[英]Why can't I call this simple web service?

I have created a web project on Eclipse with one class: "HelloWorld.java", that it's supposed to have a method that answers GET requests. 我已经在Eclipse上使用一个类“ HelloWorld.java”创建了一个Web项目,该项目应该具有一种可以回答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>";
    }
}

Then, in the WebContent folder, in the WEB-INF directory, I have created a web.xml file with the following content in order to map requests to the /hello url to my servlet. 然后,在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>

I export the project to a .war file and then I deploy it with Glassfish, but when I call the URL that supposedly calls my web service it shows me "The requested resource () is not available". 我将项目导出到.war文件,然后使用Glassfish进行部署,但是当我调用应该调用我的Web服务的URL时,它将显示“请求的资源()不可用”。

The URL I am calling is: http://localhost:8080/Calculator/hello/base 我正在调用的URL是: http:// localhost:8080 / Calculator / hello / base

Why is my web service not being called? 为什么没有调用我的Web服务?

As VGR pointed out in the comments, I was confusing JAX-RS with Servlets. 正如VGR在评论中指出的那样,我将JAX-RS与Servlet混淆了。

I chose to go with the servlet route. 我选择使用servlet路由。 I removed all annotations and replaced my getHTML method with an override of the doGet method of HttpServlet. 我删除了所有注释并将getHTML方法替换为HttpServlet的doGet方法的替代。 Everything is working now as expected. 现在一切都按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM