Web服务项目的端点无法访问。

huangapple go评论69阅读模式
英文:

Web Service Project endpoints are not accessible

问题

我有一个可以运行在Tomcat v7上的Web服务可以成功地暴露端点 `http://localhost:8080/WS-LakeshoreMarketplace/HelloWorld`。具体细节在我的主类中有描述:

```java
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错误。这在我尝试通过浏览器和Postman访问时都会发生。如何能够成功地按预期在 localhost:8080/employee/example 上暴露此端点?


<details>
<summary>英文:</summary>
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:
```java
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(&quot;text/html&quot;);
PrintWriter out = response.getWriter();
out.println(&quot;&lt;html&gt;&quot;);
out.println(&quot;&lt;head&gt;&lt;title&gt;Hello, World!&lt;/title&gt;&lt;/head&gt;&quot;);
out.println(&quot;&lt;body&gt;&quot;);
out.println(&quot;&lt;h1&gt;Hello, Nadya, Nathan, and Vismark!!!&lt;/h1&gt;&quot;);
out.println(&quot;&lt;/body&gt;&quot;);
out.println(&quot;&lt;/html&gt;&quot;);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(&quot;json&quot;);
PrintWriter out = response.getWriter();
out.println(&quot;{\&quot;message\&quot;: \&quot;This is working!!!!!!\&quot;}&quot;);
}
}

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(&quot;/employee&quot;)
public class CustomerController {

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

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

@ApplicationPath(&quot;/&quot;)
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?

答案1

得分: 2

首先,您的项目中没有包含任何支持Jersey的JAR文件。

注意:这是Jersey工作的先决条件。

从这里获取新项目: https://github.com/anish-fullstack/WS-LakeshoreMarketplace

现在,我正在使用Tomcat 7和JDK 8(JDK不会有任何问题)。更改JDK。

更新后的项目结构:

Web服务项目的端点无法访问。

由于您正在使用@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: 服务器版本名称:Apache Tomcat/7.0.106
Oct 03, 2020 12:21:33 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: 服务器构建时间:2020年9月16日 08:33:41 UTC
...(日志信息继续)...

访问URL的屏幕截图:

Web服务项目的端点无法访问。

就是这些。

英文:

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 :

Web服务项目的端点无法访问。

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

Updated web.xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&gt;
&lt;display-name&gt;WS-LakeshoreMarketplace&lt;/display-name&gt;
&lt;/web-app&gt;

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 [&quot;http-bio-8080&quot;]
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 [&quot;http-bio-8080&quot;]
Oct 03, 2020 12:21:34 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 860 ms

Screenshot of accessing the URL :

Web服务项目的端点无法访问。

That's all.

huangapple
  • 本文由 发表于 2020年10月3日 11:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64180533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定