无法搭建Jersey Hello World应用,求助排查ResourceConfig异常
Let's break down how to resolve this common Jersey startup error—this happens when Jersey can't locate your annotated resource classes. Here's a step-by-step guide tailored to your setup:
1. Verify Your Resource Class Annotations
First, double-check that your JerseyHelloWorldService class has the correct JAX-RS annotations. It needs to be a public class with a class-level @Path annotation, and at least one method with an HTTP verb annotation like @GET. Example:
package com.yourproject.resources; // Replace with your actual package import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/show-on-screen") public class JerseyHelloWorldService { @GET @Path("/{message}") @Produces(MediaType.TEXT_PLAIN) public String getMessage(@PathParam("message") String message) { return "Hello: " + message; } }
Make sure there are no typos in annotations, and the class isn't marked as private or protected.
2. Correct web.xml Configuration
Your web.xml must tell Jersey which packages to scan for resource classes—this is the most common cause of this error. Here's the proper configuration for Jersey 1.17.1:
<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_3_1.xsd" version="3.1"> <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <!-- Critical: Specify the package containing your resource class --> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.yourproject.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/rest-points/*</url-pattern> </servlet-mapping> </web-app>
Replace com.yourproject.resources with the actual package where your JerseyHelloWorldService resides. Without this line, Jersey won't scan for your resource classes.
3. Validate pom.xml Dependencies
Ensure your pom.xml includes the correct Jersey 1.17.1 dependencies without conflicts. Here's a minimal working set:
<dependencies> <!-- Jersey Core --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.17.1</version> </dependency> <!-- Jersey Server --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.17.1</version> </dependency> <!-- Jersey Servlet Integration --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.17.1</version> </dependency> <!-- JAX-RS API --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> </dependencies>
Check for any conflicting dependencies (e.g., newer Jersey versions or other JAX-RS implementations) that might interfere with classloading.
4. Confirm Project Structure & Class Compilation
- Make sure your resource class is placed under the package you specified in
web.xml(e.g.,src/main/java/com/yourproject/resources). - After building your project, check the
target/classesdirectory to ensureJerseyHelloWorldService.classis present. If it's missing, fix your build configuration to compile the class correctly.
5. Verify Access URL
Your access URL should align with this pattern:http://localhost:8080/[context-root]/[servlet-mapping]/[resource-path]/[method-path]
In your case, that translates to:http://localhost:8080/jersey-logging-demo/rest-points/show-on-screen/something
Double-check that this matches your resource class's @Path values and servlet mapping.
Additional Troubleshooting Tips
- If you're using a custom
ResourceConfigsubclass instead of package scanning, ensure you explicitly register your resource class:public class MyResourceConfig extends PackagesResourceConfig { public MyResourceConfig() { super("com.yourproject.resources"); // Or explicitly add: // getClasses().add(JerseyHelloWorldService.class); } } - Check your server logs for underlying classloading errors that might prevent Jersey from finding your resource class.
- Don't stress about the tutorial source being 404—you can build a working example using the code snippets above.
Once you've addressed these points, restart your server and test the endpoint again. This should resolve the ContainerException and get your Hello World app running.
内容的提问来源于stack exchange,提问作者Sandeepan Nath




