javarestweb-servicesjax-rsglassfish-2.x

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.

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.

<?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".

The URL I am calling is: http://localhost:8080/Calculator/hello/base

Why is my web service not being called?


Solution

  • As VGR pointed out in the comments, I was confusing JAX-RS with Servlets.

    I chose to go with the servlet route. I removed all annotations and replaced my getHTML method with an override of the doGet method of HttpServlet. Everything is working now as expected.