0
# Resource Configuration
1
2
Primary configuration class for Jersey server applications, providing comprehensive resource registration, property management, package scanning, and feature configuration capabilities. ResourceConfig serves as the central configuration hub for any Jersey server application.
3
4
## Capabilities
5
6
### ResourceConfig Class
7
8
Main configuration class extending JAX-RS Application with additional Jersey-specific configuration capabilities. Provides fluent API for comprehensive server setup.
9
10
```java { .api }
11
/**
12
* The resource configuration for configuring a web application.
13
* Extends Application and implements multiple configuration interfaces.
14
*/
15
public class ResourceConfig extends Application
16
implements Configurable<ResourceConfig>, ServerConfig, ApplicationSupplier {
17
18
// Constructors
19
/**
20
* Create a new resource configuration with default settings.
21
*/
22
public ResourceConfig();
23
24
/**
25
* Create a new resource configuration initialized with a given set of resource classes.
26
* @param classes Set of resource classes to register
27
*/
28
public ResourceConfig(Set<Class<?>> classes);
29
30
/**
31
* Create a new resource configuration initialized with given resource classes.
32
* @param classes Variable arguments of resource classes to register
33
*/
34
public ResourceConfig(Class<?>... classes);
35
36
/**
37
* Create a defensive resource configuration copy initialized with a given ResourceConfig.
38
* @param original Original ResourceConfig to copy from
39
*/
40
public ResourceConfig(ResourceConfig original);
41
42
// Factory methods
43
/**
44
* Create ResourceConfig from existing JAX-RS Application instance.
45
* @param application JAX-RS Application to wrap
46
* @return ResourceConfig configured with the application
47
*/
48
public static ResourceConfig forApplication(Application application);
49
50
/**
51
* Create ResourceConfig from JAX-RS Application class.
52
* @param applicationClass Application class to instantiate and wrap
53
* @return ResourceConfig configured with the application class
54
*/
55
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass);
56
57
/**
58
* Create ResourceConfig from JAX-RS Application class with custom properties.
59
* @param applicationClass Application class to instantiate and wrap
60
* @param properties Additional properties to configure
61
* @return ResourceConfig configured with the application class and properties
62
*/
63
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass,
64
Set<String> properties);
65
}
66
```
67
68
**Usage Example:**
69
70
```java
71
import org.glassfish.jersey.server.ResourceConfig;
72
import jakarta.ws.rs.Path;
73
import jakarta.ws.rs.GET;
74
75
@Path("/api")
76
public class ApiResource {
77
@GET
78
public String hello() {
79
return "Hello from API";
80
}
81
}
82
83
// Basic configuration
84
ResourceConfig config = new ResourceConfig();
85
config.register(ApiResource.class);
86
87
// Configuration with initial classes
88
ResourceConfig config2 = new ResourceConfig(ApiResource.class, OtherResource.class);
89
90
// Configuration with class set
91
Set<Class<?>> resourceClasses = Set.of(ApiResource.class, OtherResource.class);
92
ResourceConfig config3 = new ResourceConfig(resourceClasses);
93
94
// Copy configuration
95
ResourceConfig configCopy = new ResourceConfig(config);
96
97
// Factory methods for existing applications
98
ResourceConfig fromApp = ResourceConfig.forApplication(myApplication);
99
ResourceConfig fromClass = ResourceConfig.forApplicationClass(MyApplication.class);
100
```
101
102
### Resource Registration
103
104
Methods for registering JAX-RS resources, providers, and components with the Jersey server application.
105
106
```java { .api }
107
/**
108
* Register a class as a component with default priority.
109
* @param componentClass Class to register
110
* @return Updated ResourceConfig instance for method chaining
111
*/
112
public ResourceConfig register(Class<?> componentClass);
113
114
/**
115
* Register a class as a component with specific binding priority.
116
* @param componentClass Class to register
117
* @param priority Binding priority for the component
118
* @return Updated ResourceConfig instance for method chaining
119
*/
120
public ResourceConfig register(Class<?> componentClass, int priority);
121
122
/**
123
* Register a class as a component bound to specific contracts.
124
* @param componentClass Class to register
125
* @param contracts Set of contracts the component should be bound to
126
* @return Updated ResourceConfig instance for method chaining
127
*/
128
public ResourceConfig register(Class<?> componentClass, Class<?>... contracts);
129
130
/**
131
* Register an instance as a component.
132
* @param component Instance to register
133
* @return Updated ResourceConfig instance for method chaining
134
*/
135
public ResourceConfig register(Object component);
136
137
/**
138
* Register an instance as a component with specific binding priority.
139
* @param component Instance to register
140
* @param priority Binding priority for the component
141
* @return Updated ResourceConfig instance for method chaining
142
*/
143
public ResourceConfig register(Object component, int priority);
144
145
/**
146
* Register multiple classes at once.
147
* @param classes Set of classes to register
148
* @return Updated ResourceConfig instance for method chaining
149
*/
150
public ResourceConfig registerClasses(Set<Class<?>> classes);
151
152
/**
153
* Register multiple classes at once.
154
* @param classes Variable arguments of classes to register
155
* @return Updated ResourceConfig instance for method chaining
156
*/
157
public ResourceConfig registerClasses(Class<?>... classes);
158
```
159
160
**Usage Examples:**
161
162
```java
163
import org.glassfish.jersey.server.ResourceConfig;
164
import jakarta.ws.rs.ext.MessageBodyReader;
165
import jakarta.ws.rs.ext.MessageBodyWriter;
166
167
// Register resources and providers
168
ResourceConfig config = new ResourceConfig();
169
170
// Register resource classes
171
config.register(UserResource.class)
172
.register(OrderResource.class)
173
.register(ProductResource.class);
174
175
// Register provider with priority
176
config.register(CustomMessageBodyReader.class, 1000);
177
178
// Register provider instance
179
config.register(new CustomExceptionMapper());
180
181
// Register multiple classes
182
config.registerClasses(UserService.class, OrderService.class, ProductService.class);
183
184
// Register with specific contracts
185
config.register(MultiPurposeProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
186
```
187
188
### Package Scanning
189
190
Automatic resource and provider discovery through package scanning capabilities.
191
192
```java { .api }
193
/**
194
* Set the recursively scanned packages for resource and provider discovery.
195
* @param packages Variable arguments of package names to scan
196
* @return Updated ResourceConfig instance for method chaining
197
*/
198
public ResourceConfig packages(String... packages);
199
200
/**
201
* Set the packages for resource and provider discovery with recursion control.
202
* @param recursive Whether to scan packages recursively
203
* @param packages Variable arguments of package names to scan
204
* @return Updated ResourceConfig instance for method chaining
205
*/
206
public ResourceConfig packages(boolean recursive, String... packages);
207
208
/**
209
* Set the resource and provider class names directly.
210
* @param classNames Set of fully qualified class names
211
* @return Updated ResourceConfig instance for method chaining
212
*/
213
public ResourceConfig registerClasses(String... classNames);
214
```
215
216
**Usage Examples:**
217
218
```java
219
import org.glassfish.jersey.server.ResourceConfig;
220
221
ResourceConfig config = new ResourceConfig();
222
223
// Scan specific packages recursively (default)
224
config.packages("com.example.resources", "com.example.providers");
225
226
// Scan packages non-recursively
227
config.packages(false, "com.example.api.v1", "com.example.api.v2");
228
229
// Scan root package recursively
230
config.packages("com.example");
231
232
// Register specific classes by name
233
config.registerClasses(
234
"com.example.resources.UserResource",
235
"com.example.resources.OrderResource",
236
"com.example.providers.JsonProvider"
237
);
238
```
239
240
### Property Management
241
242
Configuration property management for customizing Jersey server behavior.
243
244
```java { .api }
245
/**
246
* Set a property value in the configuration.
247
* @param name Property name
248
* @param value Property value
249
* @return Updated ResourceConfig instance for method chaining
250
*/
251
public ResourceConfig property(String name, Object value);
252
253
/**
254
* Set multiple properties at once.
255
* @param properties Map of property names to values
256
* @return Updated ResourceConfig instance for method chaining
257
*/
258
public ResourceConfig setProperties(Map<String, ?> properties);
259
260
/**
261
* Get property value by name.
262
* @param name Property name
263
* @return Property value or null if not found
264
*/
265
public Object getProperty(String name);
266
267
/**
268
* Get all property names.
269
* @return Set of all property names
270
*/
271
public Set<String> getPropertyNames();
272
273
/**
274
* Get all properties as a map.
275
* @return Map of all properties
276
*/
277
public Map<String, Object> getProperties();
278
```
279
280
**Usage Examples:**
281
282
```java
283
import org.glassfish.jersey.server.ResourceConfig;
284
import org.glassfish.jersey.server.ServerProperties;
285
import java.util.HashMap;
286
import java.util.Map;
287
288
ResourceConfig config = new ResourceConfig();
289
290
// Set individual properties
291
config.property(ServerProperties.BV_FEATURE_DISABLE, true)
292
.property(ServerProperties.WADL_FEATURE_DISABLE, false)
293
.property(ServerProperties.PROVIDER_SCANNING_RECURSIVE, true);
294
295
// Set multiple properties
296
Map<String, Object> props = new HashMap<>();
297
props.put(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 8192);
298
props.put(ServerProperties.REDUCE_CONTEXT_PATH_SLASHES_ENABLED, true);
299
props.put("jersey.config.server.tracing", "ALL");
300
config.setProperties(props);
301
302
// Retrieve property values
303
Boolean bvDisabled = (Boolean) config.getProperty(ServerProperties.BV_FEATURE_DISABLE);
304
Set<String> allPropertyNames = config.getPropertyNames();
305
Map<String, Object> allProperties = config.getProperties();
306
```
307
308
### ServerConfig Interface
309
310
Server-side configuration interface providing access to runtime configuration information.
311
312
```java { .api }
313
/**
314
* Server-side configuration interface extending ExtendedConfig.
315
* Provides access to server-specific configuration properties and components.
316
*/
317
public interface ServerConfig extends ExtendedConfig {
318
/**
319
* Get the server-side component provider.
320
* @return Component provider instance
321
*/
322
ComponentProvider getComponentProvider();
323
324
/**
325
* Get the server runtime type.
326
* @return Always returns RuntimeType.SERVER
327
*/
328
RuntimeType getRuntimeType();
329
}
330
```
331
332
### Configuration Builder Pattern
333
334
Advanced configuration using builder patterns and fluent interfaces.
335
336
```java { .api }
337
/**
338
* Static factory methods for creating ResourceConfig instances.
339
*/
340
public static ResourceConfig forApplication(Application application);
341
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass);
342
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass,
343
Set<Class<?>> defaultClasses);
344
```
345
346
**Usage Examples:**
347
348
```java
349
import org.glassfish.jersey.server.ResourceConfig;
350
import jakarta.ws.rs.core.Application;
351
352
// Create from existing Application
353
MyApplication app = new MyApplication();
354
ResourceConfig config = ResourceConfig.forApplication(app);
355
356
// Create from Application class
357
ResourceConfig config2 = ResourceConfig.forApplicationClass(MyApplication.class);
358
359
// Create from Application class with default classes
360
Set<Class<?>> defaultClasses = Set.of(DefaultResource.class, DefaultProvider.class);
361
ResourceConfig config3 = ResourceConfig.forApplicationClass(
362
MyApplication.class,
363
defaultClasses
364
);
365
366
// Fluent configuration chaining
367
ResourceConfig fullConfig = new ResourceConfig()
368
.packages("com.example.resources", "com.example.providers")
369
.register(CustomFeature.class)
370
.register(new GlobalExceptionMapper())
371
.property(ServerProperties.BV_FEATURE_DISABLE, false)
372
.property(ServerProperties.WADL_FEATURE_DISABLE, true);
373
```