基于Eclipse+Tomcat开发Servlet出现404错误求助
Hey there, let's break down why you're seeing that 404 when accessing http://localhost:8080/MosheAPI_Project/rest/hello—it's almost always a mapping or configuration issue with JAX-RS. Let's go through the common fixes step by step.
First, Let's Clarify Your Code Context
From what you shared, your servlet has both @Path("/hello") (JAX-RS annotation) and @WebServlet("/hello") (standard Servlet annotation)—that's a red flag right there. Let's fix that and other potential issues.
1. Remove Conflicting Annotations
JAX-RS uses its own routing system, so mixing it with @WebServlet will cause mapping conflicts. Delete the @WebServlet("/hello") annotation from your servlet class. Your servlet should only use JAX-RS annotations for routing.
2. Add a Proper JAX-RS Application Class
You mentioned @ApplicationPath, but you need a dedicated class to define your API's root path. Create this class in your MyPackage:
package MyPackage; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/rest") // This sets your API's base path public class MyApiApplication extends Application { // No need for extra code here—JAX-RS will auto-scan your package for resources }
This tells JAX-RS that all your @Path endpoints will be under /rest (matching the URL you're trying to access).
3. Verify Your Servlet Class is Complete
Make sure your hello endpoint has the proper JAX-RS annotations for HTTP method and response type. Here's a full working example:
package MyPackage; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class HelloApi { @GET // Specifies this responds to GET requests @Produces(MediaType.TEXT_PLAIN) // Sets the response content type public String getHelloMessage() { return "Hello from your JAX-RS API!"; } }
4. Check Web.xml (If You're Using It)
If you're not relying on annotation-based configuration (the Application class above), you need to configure the JAX-RS servlet in web.xml. Here's the correct setup (assuming you're using Jersey, the most common JAX-RS implementation):
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <!-- Tell Jersey which package to scan for JAX-RS resources --> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>MyPackage</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> <!-- Matches your base path --> </servlet-mapping> </web-app>
Note: Make sure you have Jersey's core JARs in your project's WEB-INF/lib folder (or added via Maven/Gradle).
5. Double-Check Deployment & Context Path
- Confirm your
MosheAPI_Projectis successfully deployed to Tomcat: Check the Tomcatwebappsfolder—you should see aMosheAPI_Projectdirectory there. - Verify the context root: In Eclipse, right-click your project → Properties → Web Project Settings—ensure the Context Root is set to
MosheAPI_Project(matches your URL). - Restart Tomcat after making configuration changes—sometimes changes don't take effect until the server is restarted.
Once you've applied these fixes, try accessing http://localhost:8080/MosheAPI_Project/rest/hello again—it should return your hello message instead of a 404.
内容的提问来源于stack exchange,提问作者Yuri Zach

