Dropwizard Jersey Support - Jersey integration module for the Dropwizard Java framework
—
Core Jersey framework integration providing the main configuration classes and environment setup for Dropwizard applications. These components serve as the foundation for all Jersey-based REST services in Dropwizard.
Main Jersey resource configuration class that extends Jersey's ResourceConfig with Dropwizard-specific providers, features, and metrics integration.
/**
* Main Jersey resource configuration for Dropwizard applications
* Extends ResourceConfig with Dropwizard-specific providers and features
*/
public class DropwizardResourceConfig extends ResourceConfig {
/** Creates a new DropwizardResourceConfig with default MetricRegistry */
public DropwizardResourceConfig();
/** Creates a new DropwizardResourceConfig with the specified MetricRegistry */
public DropwizardResourceConfig(MetricRegistry metricRegistry);
/** Creates a DropwizardResourceConfig configured for testing with random port */
public static DropwizardResourceConfig forTesting();
/** Creates a testing DropwizardResourceConfig with the specified MetricRegistry */
public static DropwizardResourceConfig forTesting(MetricRegistry metricRegistry);
/** Gets the URL pattern for Jersey servlet mapping */
public String getUrlPattern();
/** Sets the URL pattern for Jersey servlet mapping */
public void setUrlPattern(String urlPattern);
/** Gets the context path for the application */
public String getContextPath();
/** Sets the context path for the application */
public void setContextPath(String contextPath);
/** Gets formatted information about registered endpoints */
public String getEndpointsInfo();
/** Registers a component with enhanced binding support */
public ResourceConfig register(Object component);
}Usage Examples:
import io.dropwizard.jersey.DropwizardResourceConfig;
import com.codahale.metrics.MetricRegistry;
// Basic configuration
DropwizardResourceConfig config = new DropwizardResourceConfig();
// With custom metrics
MetricRegistry metrics = new MetricRegistry();
DropwizardResourceConfig config = new DropwizardResourceConfig(metrics);
// Configure URL pattern
config.setUrlPattern("/api/*");
config.setContextPath("/myapp");
// Register resources
config.register(UserResource.class);
config.register(new OrderService());
// For testing
DropwizardResourceConfig testConfig = DropwizardResourceConfig.forTesting();Facade for Jersey configuration that provides a simplified interface for common Jersey operations and resource registration.
/**
* Jersey environment configuration facade providing simplified Jersey configuration
*/
public class JerseyEnvironment {
/** Disables Jersey by setting container to null */
public void disable();
/** Replaces the Jersey servlet container with a custom implementation */
public void replace(Function<ResourceConfig, Servlet> replace);
/** Registers an object as a Jersey singleton component */
public void register(Object component);
/** Registers a class as a Jersey component (must have no-args constructor or use DI) */
public void register(Class<?> componentClass);
/** Adds package names to scan for Jersey components recursively */
public void packages(String... packages);
/** Enables a Jersey feature by name */
public void enable(String featureName);
/** Disables a Jersey feature by name */
public void disable(String featureName);
/** Sets a Jersey property */
public void property(String name, Object value);
/** Gets a Jersey property value */
public <T> T getProperty(String name);
/** Gets the current URL pattern */
public String getUrlPattern();
/** Sets the URL pattern, automatically adding /* suffix if needed */
public void setUrlPattern(String urlPattern);
/** Gets the underlying DropwizardResourceConfig */
public DropwizardResourceConfig getResourceConfig();
}Usage Examples:
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.jersey.setup.JerseyContainerHolder;
// Create Jersey environment
JerseyContainerHolder holder = new JerseyContainerHolder();
DropwizardResourceConfig config = new DropwizardResourceConfig();
JerseyEnvironment jersey = new JerseyEnvironment(holder, config);
// Register components
jersey.register(UserResource.class);
jersey.register(new CustomProvider());
// Package scanning
jersey.packages("com.example.resources", "com.example.providers");
// Feature configuration
jersey.enable("com.example.CustomFeature");
jersey.property("jersey.config.server.wadl.disableWadl", true);
// URL configuration
jersey.setUrlPattern("/api/v1/*");Container holder that manages the Jersey servlet container instance and allows for dynamic replacement.
/**
* Holds reference to Jersey servlet container allowing dynamic replacement
*/
public class JerseyContainerHolder {
/** Sets the Jersey servlet container */
public void setContainer(Servlet container);
/** Gets the current Jersey servlet container */
public Servlet getContainer();
}Custom servlet container that extends Jersey's ServletContainer with Dropwizard-specific enhancements.
/**
* Custom servlet container extending Jersey's ServletContainer
* Provides Dropwizard-specific servlet integration
*/
public class JerseyServletContainer extends ServletContainer {
/** Creates JerseyServletContainer with DropwizardResourceConfig */
public JerseyServletContainer(DropwizardResourceConfig resourceConfig);
}public class MyApplication extends Application<MyConfiguration> {
@Override
public void run(MyConfiguration config, Environment environment) {
// Jersey is automatically configured by Dropwizard
JerseyEnvironment jersey = environment.jersey();
// Register resources
jersey.register(UserResource.class);
jersey.register(OrderResource.class);
// Configure URL pattern
jersey.setUrlPattern("/api/*");
}
}public class CustomJerseySetup {
public void configure(Environment environment) {
JerseyEnvironment jersey = environment.jersey();
// Get direct access to ResourceConfig for advanced configuration
DropwizardResourceConfig config = jersey.getResourceConfig();
// Register custom components
config.register(new CustomBinder());
config.register(CustomFeature.class);
// Configure properties
config.property(ServerProperties.WADL_FEATURE_DISABLE, true);
config.property(ServerProperties.BV_FEATURE_DISABLE, false);
}
}public class ResourceTest extends JerseyTest {
@Override
protected Application configure() {
return DropwizardResourceConfig.forTesting()
.register(UserResource.class)
.register(TestBinder.class);
}
@Test
public void testUserEndpoint() {
Response response = target("/users/123").request().get();
assertEquals(200, response.getStatus());
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-jersey