Java REST服务URL映射问题:调用指定路径返回404错误
Hey there! Let's troubleshoot that 404 error with your REST service step by step. Since you didn't share your actual service code and web.xml, I'll walk through the most common issues that cause this kind of URL mapping failure for Java REST services (assuming you're using JAX-RS like Jersey or RESTEasy, which is standard for this use case):
1. Verify Your Servlet Mapping in web.xml
This is the #1 culprit for URL mapping issues. Check if your JAX-RS servlet is mapped to the right path that includes /square:
- If your servlet is only mapped to a prefix like
/rest/*, your URL should behttp://localhost:8080/MyProjectName/rest/square/3instead. - Here's a correct
web.xmlexample for Jersey:<servlet> <servlet-name>MyRESTService</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.yourpackage.rest</param-value> <!-- Replace with your service's package --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyRESTService</servlet-name> <url-pattern>/square/*</url-pattern> <!-- Maps all /square paths to your servlet --> </servlet-mapping> - If you're using annotation-driven configuration (with
@ApplicationPath), make sure your application class is set up correctly:@ApplicationPath("/") // Or use "/square" if you want that as the base path public class MyRESTApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<>(); classes.add(SquareService.class); // Your service class return classes; } }
2. Check Your Service Class Annotations
Double-check that your REST service class and method have the right @Path annotations:
- For the URL
http://localhost:8080/MyProjectName/square/3, your service should look like this:@Path("/square") public class SquareService { @GET @Path("/{num}") @Produces(MediaType.TEXT_PLAIN) public String calculateSquare(@PathParam("num") int number) { return String.valueOf(number * number); } } - Note: If your
@ApplicationPathis already set to/square, you can omit the class-level@Pathand just use@Path("/{num}")on the method.
3. Confirm the Deployment Context Path
Make sure your project is deployed with the context path MyProjectName. Sometimes IDEs or servers auto-modify this (e.g., using lowercase, removing hyphens, or using a different name). Check:
- Your server's
webappsdirectory (for Tomcat) to see if your project folder is namedMyProjectName. - Your IDE's deployment settings to confirm the context path matches what you're using in the URL.
4. Ensure JAX-RS Dependencies Are Included
If your project is missing the JAX-RS implementation (like Jersey or RESTEasy), the server won't recognize your REST annotations, so the service won't be registered. For Maven, add this dependency (Jersey example):
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.41</version> <!-- Use the latest stable version --> </dependency>
5. Check Server Startup Logs
Sometimes a 404 hides an underlying startup error. Look at your server's logs (e.g., Tomcat's catalina.out) for errors like missing classes, annotation parsing issues, or configuration mistakes—these can prevent your service from being initialized.
Start with the servlet mapping and service annotations first—those are the most frequent causes. If you can share your actual code and web.xml, I can give you more targeted fixes!
内容的提问来源于stack exchange,提问作者Eutherpy

