簡體   English   中英

無法訪問 Web 服務項目端點

[英]Web Service Project endpoints are not accessible

我有一個可以在 Tomcat v7 上運行的 web 服務,並且可以成功公開端點http://localhost:8080/WS-LakeshoreMarketplace/HelloWorld 詳細信息在下面我的主類中描述:

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!!!!!!\"}");
    }

}

現在,我創建了一個控制器類來保存我想公開的新端點,這個類:

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.";
    }
}

我已經添加了以下用@ApplicationPath注釋的類,但它仍然不起作用:

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

但是當我嘗試通過 GET 請求訪問localhost:8080/employee/example端點時,我收到 404 錯誤。 當我嘗試通過瀏覽器和郵遞員訪問時,就會發生這種情況。 如何按預期在localhost:8080/employee/example上成功公開此端點?

首先,您的項目不包含任何支持球衣的罐子。

注意:這是球衣工作的先決條件。

從這里拉出新項目: https : //github.com/anish-fullstack/WS-LakeshoreMarketplace

現在,我使用的是 tomcat 7 和 JDK 8(jdk 不會有任何問題)。 更改 JDK。

更新的項目結構:

在此處輸入圖片說明

當您使用@ApplicationPath 時,請不要在 web.xml 中指定任何內容

更新的 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>

成功 Tomcat 7 日志:

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

訪問 URL 的屏幕截圖:

在此處輸入圖片說明

就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM