Jersey core server implementation for building RESTful Web Services with JAX-RS.
—
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.
Main configuration class extending JAX-RS Application with additional Jersey-specific configuration capabilities. Provides fluent API for comprehensive server setup.
/**
* The resource configuration for configuring a web application.
* Extends Application and implements multiple configuration interfaces.
*/
public class ResourceConfig extends Application
implements Configurable<ResourceConfig>, ServerConfig, ApplicationSupplier {
// Constructors
/**
* Create a new resource configuration with default settings.
*/
public ResourceConfig();
/**
* Create a new resource configuration initialized with a given set of resource classes.
* @param classes Set of resource classes to register
*/
public ResourceConfig(Set<Class<?>> classes);
/**
* Create a new resource configuration initialized with given resource classes.
* @param classes Variable arguments of resource classes to register
*/
public ResourceConfig(Class<?>... classes);
/**
* Create a defensive resource configuration copy initialized with a given ResourceConfig.
* @param original Original ResourceConfig to copy from
*/
public ResourceConfig(ResourceConfig original);
// Factory methods
/**
* Create ResourceConfig from existing JAX-RS Application instance.
* @param application JAX-RS Application to wrap
* @return ResourceConfig configured with the application
*/
public static ResourceConfig forApplication(Application application);
/**
* Create ResourceConfig from JAX-RS Application class.
* @param applicationClass Application class to instantiate and wrap
* @return ResourceConfig configured with the application class
*/
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass);
/**
* Create ResourceConfig from JAX-RS Application class with custom properties.
* @param applicationClass Application class to instantiate and wrap
* @param properties Additional properties to configure
* @return ResourceConfig configured with the application class and properties
*/
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass,
Set<String> properties);
}Usage Example:
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.GET;
@Path("/api")
public class ApiResource {
@GET
public String hello() {
return "Hello from API";
}
}
// Basic configuration
ResourceConfig config = new ResourceConfig();
config.register(ApiResource.class);
// Configuration with initial classes
ResourceConfig config2 = new ResourceConfig(ApiResource.class, OtherResource.class);
// Configuration with class set
Set<Class<?>> resourceClasses = Set.of(ApiResource.class, OtherResource.class);
ResourceConfig config3 = new ResourceConfig(resourceClasses);
// Copy configuration
ResourceConfig configCopy = new ResourceConfig(config);
// Factory methods for existing applications
ResourceConfig fromApp = ResourceConfig.forApplication(myApplication);
ResourceConfig fromClass = ResourceConfig.forApplicationClass(MyApplication.class);Methods for registering JAX-RS resources, providers, and components with the Jersey server application.
/**
* Register a class as a component with default priority.
* @param componentClass Class to register
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig register(Class<?> componentClass);
/**
* Register a class as a component with specific binding priority.
* @param componentClass Class to register
* @param priority Binding priority for the component
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig register(Class<?> componentClass, int priority);
/**
* Register a class as a component bound to specific contracts.
* @param componentClass Class to register
* @param contracts Set of contracts the component should be bound to
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig register(Class<?> componentClass, Class<?>... contracts);
/**
* Register an instance as a component.
* @param component Instance to register
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig register(Object component);
/**
* Register an instance as a component with specific binding priority.
* @param component Instance to register
* @param priority Binding priority for the component
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig register(Object component, int priority);
/**
* Register multiple classes at once.
* @param classes Set of classes to register
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig registerClasses(Set<Class<?>> classes);
/**
* Register multiple classes at once.
* @param classes Variable arguments of classes to register
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig registerClasses(Class<?>... classes);Usage Examples:
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
// Register resources and providers
ResourceConfig config = new ResourceConfig();
// Register resource classes
config.register(UserResource.class)
.register(OrderResource.class)
.register(ProductResource.class);
// Register provider with priority
config.register(CustomMessageBodyReader.class, 1000);
// Register provider instance
config.register(new CustomExceptionMapper());
// Register multiple classes
config.registerClasses(UserService.class, OrderService.class, ProductService.class);
// Register with specific contracts
config.register(MultiPurposeProvider.class, MessageBodyReader.class, MessageBodyWriter.class);Automatic resource and provider discovery through package scanning capabilities.
/**
* Set the recursively scanned packages for resource and provider discovery.
* @param packages Variable arguments of package names to scan
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig packages(String... packages);
/**
* Set the packages for resource and provider discovery with recursion control.
* @param recursive Whether to scan packages recursively
* @param packages Variable arguments of package names to scan
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig packages(boolean recursive, String... packages);
/**
* Set the resource and provider class names directly.
* @param classNames Set of fully qualified class names
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig registerClasses(String... classNames);Usage Examples:
import org.glassfish.jersey.server.ResourceConfig;
ResourceConfig config = new ResourceConfig();
// Scan specific packages recursively (default)
config.packages("com.example.resources", "com.example.providers");
// Scan packages non-recursively
config.packages(false, "com.example.api.v1", "com.example.api.v2");
// Scan root package recursively
config.packages("com.example");
// Register specific classes by name
config.registerClasses(
"com.example.resources.UserResource",
"com.example.resources.OrderResource",
"com.example.providers.JsonProvider"
);Configuration property management for customizing Jersey server behavior.
/**
* Set a property value in the configuration.
* @param name Property name
* @param value Property value
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig property(String name, Object value);
/**
* Set multiple properties at once.
* @param properties Map of property names to values
* @return Updated ResourceConfig instance for method chaining
*/
public ResourceConfig setProperties(Map<String, ?> properties);
/**
* Get property value by name.
* @param name Property name
* @return Property value or null if not found
*/
public Object getProperty(String name);
/**
* Get all property names.
* @return Set of all property names
*/
public Set<String> getPropertyNames();
/**
* Get all properties as a map.
* @return Map of all properties
*/
public Map<String, Object> getProperties();Usage Examples:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import java.util.HashMap;
import java.util.Map;
ResourceConfig config = new ResourceConfig();
// Set individual properties
config.property(ServerProperties.BV_FEATURE_DISABLE, true)
.property(ServerProperties.WADL_FEATURE_DISABLE, false)
.property(ServerProperties.PROVIDER_SCANNING_RECURSIVE, true);
// Set multiple properties
Map<String, Object> props = new HashMap<>();
props.put(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 8192);
props.put(ServerProperties.REDUCE_CONTEXT_PATH_SLASHES_ENABLED, true);
props.put("jersey.config.server.tracing", "ALL");
config.setProperties(props);
// Retrieve property values
Boolean bvDisabled = (Boolean) config.getProperty(ServerProperties.BV_FEATURE_DISABLE);
Set<String> allPropertyNames = config.getPropertyNames();
Map<String, Object> allProperties = config.getProperties();Server-side configuration interface providing access to runtime configuration information.
/**
* Server-side configuration interface extending ExtendedConfig.
* Provides access to server-specific configuration properties and components.
*/
public interface ServerConfig extends ExtendedConfig {
/**
* Get the server-side component provider.
* @return Component provider instance
*/
ComponentProvider getComponentProvider();
/**
* Get the server runtime type.
* @return Always returns RuntimeType.SERVER
*/
RuntimeType getRuntimeType();
}Advanced configuration using builder patterns and fluent interfaces.
/**
* Static factory methods for creating ResourceConfig instances.
*/
public static ResourceConfig forApplication(Application application);
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass);
public static ResourceConfig forApplicationClass(Class<? extends Application> applicationClass,
Set<Class<?>> defaultClasses);Usage Examples:
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.core.Application;
// Create from existing Application
MyApplication app = new MyApplication();
ResourceConfig config = ResourceConfig.forApplication(app);
// Create from Application class
ResourceConfig config2 = ResourceConfig.forApplicationClass(MyApplication.class);
// Create from Application class with default classes
Set<Class<?>> defaultClasses = Set.of(DefaultResource.class, DefaultProvider.class);
ResourceConfig config3 = ResourceConfig.forApplicationClass(
MyApplication.class,
defaultClasses
);
// Fluent configuration chaining
ResourceConfig fullConfig = new ResourceConfig()
.packages("com.example.resources", "com.example.providers")
.register(CustomFeature.class)
.register(new GlobalExceptionMapper())
.property(ServerProperties.BV_FEATURE_DISABLE, false)
.property(ServerProperties.WADL_FEATURE_DISABLE, true);Install with Tessl CLI
npx tessl i tessl/maven-org-glassfish-jersey-core--jersey-server