Web 服务项目端点不可访问
Web Service Project endpoints are not accessible
我有一个可以在 Tomcat v7 上 运行 的网络服务,并且可以成功公开端点 http://localhost:8080/WS-LakeshoreMarketplace/HelloWorld
。这方面的细节在我下面的主要 class 中描述:
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!!!!!!\"}");
}
}
现在,我创建了一个控制器 class 来保存我想公开的新端点,这个 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.";
}
}
我还添加了以下 class 并用 @ApplicationPath
注释,但它仍然不起作用:
@ApplicationPath("/")
public class DefaultController extends Application {
}
但是当我尝试通过 GET 请求访问 localhost:8080/employee/example
端点时,出现 404 错误。当我尝试通过浏览器和 Postman 访问时,就会发生这种情况。如何按预期在 localhost:8080/employee/example
上成功公开此端点?
首先,您的项目不包含任何支持 jersey 的 jar。
注意:这是球衣工作的先决条件。
从这里拉新项目: 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截图:
就这些了。
我有一个可以在 Tomcat v7 上 运行 的网络服务,并且可以成功公开端点 http://localhost:8080/WS-LakeshoreMarketplace/HelloWorld
。这方面的细节在我下面的主要 class 中描述:
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!!!!!!\"}");
}
}
现在,我创建了一个控制器 class 来保存我想公开的新端点,这个 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.";
}
}
我还添加了以下 class 并用 @ApplicationPath
注释,但它仍然不起作用:
@ApplicationPath("/")
public class DefaultController extends Application {
}
但是当我尝试通过 GET 请求访问 localhost:8080/employee/example
端点时,出现 404 错误。当我尝试通过浏览器和 Postman 访问时,就会发生这种情况。如何按预期在 localhost:8080/employee/example
上成功公开此端点?
首先,您的项目不包含任何支持 jersey 的 jar。
注意:这是球衣工作的先决条件。
从这里拉新项目: 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截图:
就这些了。