简体   繁体   中英

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

Two simple POST and GET URLs, localhost:8080/mdht-restlet , both work perfectly when run from IntelliJ IDEA. Separately, mdht-restlet.war dropped into /opt/tomcat/webapp deploys, shows up in Tomcat Application Manager as running, no errors or warnings in catalina.out . GET and POST both produce 404. Whether I produce the WAR file from the command line, mvn package , or from IDEA, Build-> Build Artifacts... , the result is the same.

As noted, it works perfectly from IDEA Run/Debug. I'm uncertain as to what to look at. Here are some relevant details:

web.xml contents:

  <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 build plug-in for 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.";
  }
}

The answer to this is that Tomcat considers the name of the WAR file, in this case, mdht-restlet.war , to be the root (unless you rename the WAR file to ROOT.war which Tomcat deploys to ROOT ), so the URL is to start with 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 , so the shorter URL was never going to do otherwise than 404.

Why did this get by IntelliJ IDEA? Probably because of how the Run/Debug Configuration was set up between the settings in the Server tab (for Tomcat) and the Deployment tab. These did not follow (the points made in my first paragraph) and so gave a false impression of success (because they were jacked).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM