尽管 Servlet 在 IntelliJ IDEA 中工作,但在部署到 Tomcat 时无法工作

Servlet fails to work when deployed to Tomcat though it works in IntelliJ IDEA

两个简单的 POSTGET URL,localhost:8080/mdht-restlet,在来自 IntelliJ IDEA 的 运行 时都可以完美运行.另外,mdht-restlet.war 放入 /opt/tomcat/webapp 部署,在 Tomcat 应用程序管理器中显示为 运行ning,catalina.out 中没有错误或警告。 GET 和 POST 都产生 404。无论我是从命令行 mvn package 还是从 IDEA Build-> Build Artifacts... 产生 WAR 文件,结果都是一样的。

如前所述,它在 IDEA Run/Debug 中完美运行。我不确定要看什么。以下是一些相关细节:

web.xml内容:

  <servlet>
    <servlet-name>mdht-restlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.windofkeltia.servlet</param-value>
    </init-param>    
  </servlet>

  <servlet-mapping>
    <servlet-name>mdht-restlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

pom.xml 为 WAR 构建插件:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>${maven-war-plugin.version}</version>
    <configuration>
      <webResources>
        <resource>
          <!-- this is relative to the project parent directory -->
          <directory>web</directory>
        </resource>
      </webResources>
    </configuration>
  </plugin>

MdhtRestlet.java:

package com.windofkeltia.servlet;
...
@Path( "/mdht-restlet" )
public class MdhtRestlet
{
  @POST
  @Consumes( MediaType.TEXT_PLAIN )
  @Produces( MediaType.TEXT_XML )
  public Response postPatientData( @Context HttpServletRequest request, @Context HttpHeaders header )
  {
    ...
  }

  @GET
  @Produces( MediaType.TEXT_PLAIN )
  public String getStatusInPlainText()
  {
    return "The MDHT restlet is up.";
  }
}

答案是 Tomcat 考虑 WAR 文件的名称,在这种情况下,mdht-restlet.war,成为根目录(除非您将 WAR 文件重命名为 ROOT.war,Tomcat 部署到 ROOT ), 所以 URL 是从 http://localhost:8080/mdht-restlet. Adding to that, the servlet Java code uses the @PATH annotation at the class level to add to the URL. That augments it to http://localhost:8080/mdht-restlet/mdht-restlet 开始的,所以较短的 URL 除了 404.

为什么这个是IntelliJ IDEA得到的?可能是因为 Run/Debug Configuration 是如何在 Server 选项卡(对于 Tomcat)和 Deployment 选项卡中设置的。这些并没有遵循(我第一段中提出的要点),因此给人一种成功的错误印象(因为他们被劫持了)。