NetBeans 8.2中Maven Web应用Tomcat无法运行但GlassFish正常求助
Hey there! Let’s troubleshoot why your RESTful app is throwing an HTTP 404 on Tomcat but works perfectly on GlassFish. Since it’s a default Maven project, here are the most common culprits to check step by step:
Tomcat uses your WAR file’s name as the default context path unless you explicitly configure it. For example, if your WAR is named my-rest-service.war, your base request URL should be http://localhost:8080/my-rest-service/—not just http://localhost:8080/. GlassFish might have used a different default (like your project’s raw name without the .war suffix), so make sure your test URLs match Tomcat’s expected path.
web.xml) If you’re using traditional web.xml instead of Jakarta EE annotations, ensure your REST servlet is mapped correctly. For JAX-RS apps, this usually looks like:
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.yourcompany.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
If you’re using annotations like @ApplicationPath, confirm it’s set properly (e.g., @ApplicationPath("/api")) and your resource classes have @Path annotations that align with the URLs you’re testing.
Tomcat versions have strict Jakarta EE/JAX-RS support rules:
- Tomcat 9 uses Java EE 8 (JAX-RS 2.1, with
javax.*packages) - Tomcat 10+ uses Jakarta EE 9+ (JAX-RS 3.0, with
jakarta.*packages)
If your Maven dependencies are usingjavax.ws.rsbut you’re running Tomcat 10+, you’ll hit classpath issues. Check yourpom.xml—for Tomcat 10+, switch to Jakarta EE artifacts (e.g.,jakarta.ws.rs:jakarta.ws.rs-api:3.1.0instead ofjavax.ws.rs:javax.ws.rs-api:2.1.1).
Don’t overlook Tomcat’s log files! Check logs/catalina.out and localhost.log in your Tomcat installation directory. Look for class-not-found errors, deployment failures, or servlet initialization exceptions. These logs will often spell out exactly why your app isn’t starting (like missing JAX-RS implementation jars such as Jersey or RESTEasy).
Verify your pom.xml has <packaging>war</packaging> set. Run mvn clean package and inspect the generated WAR in the target folder—make sure all necessary dependencies are included. GlassFish includes some Jakarta EE libraries out of the box, so if you marked dependencies as <scope>provided</scope> for GlassFish, those jars won’t be in your WAR and Tomcat (a servlet-only container) won’t have them either.
Head to Tomcat’s Manager app (http://localhost:8080/manager/html), log in, and see if your app is listed as "Running". If it’s not started, try launching it manually—sometimes Tomcat fails to deploy apps silently due to configuration glitches, and the manager will show you the error when you attempt to start it.
One key difference between GlassFish and Tomcat: GlassFish is a full Jakarta EE server that includes JAX-RS out of the box. Tomcat is just a servlet container, so you need to include a JAX-RS implementation (like Jersey) in your WAR or install it as a Tomcat library. Double-check that those jars are present in your WEB-INF/lib folder!
内容的提问来源于stack exchange,提问作者Rieske

