Search code examples
javaweb-servicesjerseyjax-rs

Web Service Project endpoints are not accessible


I have a webservice that can run on Tomcat v7, and can successfully expose the endpoint http://localhost:8080/WS-LakeshoreMarketplace/HelloWorld. The details of this are described in my main class below:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Hello, World!</title></head>");
        out.println("<body>");
        out.println("<h1>Hello, Nadya, Nathan, and Vismark!!!</h1>");
        out.println("</body>");
        out.println("</html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("json");
        PrintWriter out = response.getWriter();
        out.println("{\"message\": \"This is working!!!!!!\"}");
    }

}

Now, I have created a controller class to hold new endpoints that I'd like to expose, this class:

package com.lakeshoremarketplace.controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/employee")
public class CustomerController {

    @GET
    @Produces({"application/json"})
    @Path("/example")
    public String getDummyEmployee() {
        return "success.";
    }
}

I've aso added the following class annotated with the @ApplicationPath, but it still does not work:

@ApplicationPath("/")
public class DefaultController extends Application {
    
}

But when I try to access the localhost:8080/employee/example endpoint via a GET request, I get a 404 error. This is happening when I try accessing through a browser, and through Postman. How can I successfully expose this endpoint on localhost:8080/employee/example as intended?


Solution

  • First of all, your project doesn't contain any of the jars to support jersey.

    Note: That's a prior requirements for jersey to work.

    Pull the new project from here : https://github.com/anish-fullstack/WS-LakeshoreMarketplace

    Now, I'm using tomcat 7 and JDK 8 (jdk will not be any issue). Change the JDK.

    Updated project structure :

    enter image description here

    As you are using @ApplicationPath, then you don't to specify anything in the web.xml

    Updated 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>WS-LakeshoreMarketplace</display-name>
    </web-app>
    

    Success Tomcat 7 log :

    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version name:   Apache Tomcat/7.0.106
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server built:          Sep 16 2020 08:33:41 UTC
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version number: 7.0.106.0
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Name:               Mac OS X
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Version:            10.15.6
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Architecture:          x86_64
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Version:           1.8.0_231-b11
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Vendor:            Oracle Corporation
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_BASE:         /Volumes/Local Disk/Software Development/Workspaces/Algorithms/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_HOME:         /Users/anish/Downloads/apache-tomcat-7.0.106
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.base=/Volumes/Local Disk/Software Development/Workspaces/Algorithms/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=/Users/anish/Downloads/apache-tomcat-7.0.106
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dwtp.deploy=/Volumes/Local Disk/Software Development/Workspaces/Algorithms/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Djava.endorsed.dirs=/Users/anish/Downloads/apache-tomcat-7.0.106/endorsed
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dfile.encoding=UTF-8
    Oct 03, 2020 12:21:33 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [/Users/anish/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
    Oct 03, 2020 12:21:33 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 415 ms
    Oct 03, 2020 12:21:33 PM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service [Catalina]
    Oct 03, 2020 12:21:33 PM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.106
    Oct 03, 2020 12:21:33 PM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
    INFO: validateJarFile(/Volumes/Local Disk/Software Development/Workspaces/Algorithms/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WS-LakeshoreMarketplace/WEB-INF/lib/javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
    Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.TldConfig execute
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Oct 03, 2020 12:21:34 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    Oct 03, 2020 12:21:34 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 860 ms
    

    Screenshot of accessing the URL :

    enter image description here

    That's all.