0
# Framework Configuration
1
2
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.
3
4
## Capabilities
5
6
### DropwizardResourceConfig
7
8
Main Jersey resource configuration class that extends Jersey's ResourceConfig with Dropwizard-specific providers, features, and metrics integration.
9
10
```java { .api }
11
/**
12
* Main Jersey resource configuration for Dropwizard applications
13
* Extends ResourceConfig with Dropwizard-specific providers and features
14
*/
15
public class DropwizardResourceConfig extends ResourceConfig {
16
17
/** Creates a new DropwizardResourceConfig with default MetricRegistry */
18
public DropwizardResourceConfig();
19
20
/** Creates a new DropwizardResourceConfig with the specified MetricRegistry */
21
public DropwizardResourceConfig(MetricRegistry metricRegistry);
22
23
/** Creates a DropwizardResourceConfig configured for testing with random port */
24
public static DropwizardResourceConfig forTesting();
25
26
/** Creates a testing DropwizardResourceConfig with the specified MetricRegistry */
27
public static DropwizardResourceConfig forTesting(MetricRegistry metricRegistry);
28
29
/** Gets the URL pattern for Jersey servlet mapping */
30
public String getUrlPattern();
31
32
/** Sets the URL pattern for Jersey servlet mapping */
33
public void setUrlPattern(String urlPattern);
34
35
/** Gets the context path for the application */
36
public String getContextPath();
37
38
/** Sets the context path for the application */
39
public void setContextPath(String contextPath);
40
41
/** Gets formatted information about registered endpoints */
42
public String getEndpointsInfo();
43
44
/** Registers a component with enhanced binding support */
45
public ResourceConfig register(Object component);
46
}
47
```
48
49
**Usage Examples:**
50
51
```java
52
import io.dropwizard.jersey.DropwizardResourceConfig;
53
import com.codahale.metrics.MetricRegistry;
54
55
// Basic configuration
56
DropwizardResourceConfig config = new DropwizardResourceConfig();
57
58
// With custom metrics
59
MetricRegistry metrics = new MetricRegistry();
60
DropwizardResourceConfig config = new DropwizardResourceConfig(metrics);
61
62
// Configure URL pattern
63
config.setUrlPattern("/api/*");
64
config.setContextPath("/myapp");
65
66
// Register resources
67
config.register(UserResource.class);
68
config.register(new OrderService());
69
70
// For testing
71
DropwizardResourceConfig testConfig = DropwizardResourceConfig.forTesting();
72
```
73
74
### JerseyEnvironment
75
76
Facade for Jersey configuration that provides a simplified interface for common Jersey operations and resource registration.
77
78
```java { .api }
79
/**
80
* Jersey environment configuration facade providing simplified Jersey configuration
81
*/
82
public class JerseyEnvironment {
83
84
/** Disables Jersey by setting container to null */
85
public void disable();
86
87
/** Replaces the Jersey servlet container with a custom implementation */
88
public void replace(Function<ResourceConfig, Servlet> replace);
89
90
/** Registers an object as a Jersey singleton component */
91
public void register(Object component);
92
93
/** Registers a class as a Jersey component (must have no-args constructor or use DI) */
94
public void register(Class<?> componentClass);
95
96
/** Adds package names to scan for Jersey components recursively */
97
public void packages(String... packages);
98
99
/** Enables a Jersey feature by name */
100
public void enable(String featureName);
101
102
/** Disables a Jersey feature by name */
103
public void disable(String featureName);
104
105
/** Sets a Jersey property */
106
public void property(String name, Object value);
107
108
/** Gets a Jersey property value */
109
public <T> T getProperty(String name);
110
111
/** Gets the current URL pattern */
112
public String getUrlPattern();
113
114
/** Sets the URL pattern, automatically adding /* suffix if needed */
115
public void setUrlPattern(String urlPattern);
116
117
/** Gets the underlying DropwizardResourceConfig */
118
public DropwizardResourceConfig getResourceConfig();
119
}
120
```
121
122
**Usage Examples:**
123
124
```java
125
import io.dropwizard.jersey.setup.JerseyEnvironment;
126
import io.dropwizard.jersey.setup.JerseyContainerHolder;
127
128
// Create Jersey environment
129
JerseyContainerHolder holder = new JerseyContainerHolder();
130
DropwizardResourceConfig config = new DropwizardResourceConfig();
131
JerseyEnvironment jersey = new JerseyEnvironment(holder, config);
132
133
// Register components
134
jersey.register(UserResource.class);
135
jersey.register(new CustomProvider());
136
137
// Package scanning
138
jersey.packages("com.example.resources", "com.example.providers");
139
140
// Feature configuration
141
jersey.enable("com.example.CustomFeature");
142
jersey.property("jersey.config.server.wadl.disableWadl", true);
143
144
// URL configuration
145
jersey.setUrlPattern("/api/v1/*");
146
```
147
148
### JerseyContainerHolder
149
150
Container holder that manages the Jersey servlet container instance and allows for dynamic replacement.
151
152
```java { .api }
153
/**
154
* Holds reference to Jersey servlet container allowing dynamic replacement
155
*/
156
public class JerseyContainerHolder {
157
158
/** Sets the Jersey servlet container */
159
public void setContainer(Servlet container);
160
161
/** Gets the current Jersey servlet container */
162
public Servlet getContainer();
163
}
164
```
165
166
### JerseyServletContainer
167
168
Custom servlet container that extends Jersey's ServletContainer with Dropwizard-specific enhancements.
169
170
```java { .api }
171
/**
172
* Custom servlet container extending Jersey's ServletContainer
173
* Provides Dropwizard-specific servlet integration
174
*/
175
public class JerseyServletContainer extends ServletContainer {
176
177
/** Creates JerseyServletContainer with DropwizardResourceConfig */
178
public JerseyServletContainer(DropwizardResourceConfig resourceConfig);
179
}
180
```
181
182
## Integration Patterns
183
184
### Basic Application Setup
185
186
```java
187
public class MyApplication extends Application<MyConfiguration> {
188
189
@Override
190
public void run(MyConfiguration config, Environment environment) {
191
// Jersey is automatically configured by Dropwizard
192
JerseyEnvironment jersey = environment.jersey();
193
194
// Register resources
195
jersey.register(UserResource.class);
196
jersey.register(OrderResource.class);
197
198
// Configure URL pattern
199
jersey.setUrlPattern("/api/*");
200
}
201
}
202
```
203
204
### Custom Jersey Configuration
205
206
```java
207
public class CustomJerseySetup {
208
209
public void configure(Environment environment) {
210
JerseyEnvironment jersey = environment.jersey();
211
212
// Get direct access to ResourceConfig for advanced configuration
213
DropwizardResourceConfig config = jersey.getResourceConfig();
214
215
// Register custom components
216
config.register(new CustomBinder());
217
config.register(CustomFeature.class);
218
219
// Configure properties
220
config.property(ServerProperties.WADL_FEATURE_DISABLE, true);
221
config.property(ServerProperties.BV_FEATURE_DISABLE, false);
222
}
223
}
224
```
225
226
### Testing Configuration
227
228
```java
229
public class ResourceTest extends JerseyTest {
230
231
@Override
232
protected Application configure() {
233
return DropwizardResourceConfig.forTesting()
234
.register(UserResource.class)
235
.register(TestBinder.class);
236
}
237
238
@Test
239
public void testUserEndpoint() {
240
Response response = target("/users/123").request().get();
241
assertEquals(200, response.getStatus());
242
}
243
}
244
```