Jersey core server implementation for building RESTful Web Services with JAX-RS.
—
Core request processing functionality including the main application handler, container request/response objects, lifecycle management, and exception handling. This system handles the complete request flow from HTTP input to JAX-RS resource method execution and response generation.
Central request processing engine that handles the complete request lifecycle from routing to response generation. Manages the entire Jersey server runtime environment.
/**
* Jersey server-side application handler.
* Provides functionality to process HTTP requests in a JAX-RS application.
*/
public final class ApplicationHandler {
/**
* Create a new Jersey application handler based on an Application.
* @param application JAX-RS application instance
*/
public ApplicationHandler(Application application);
/**
* Create a new Jersey application handler based on an Application class.
* @param applicationClass JAX-RS application class
*/
public ApplicationHandler(Class<? extends Application> applicationClass);
/**
* Process a container request and return the response asynchronously.
* @param requestContext Container request to process
* @return Future containing the container response
*/
public Future<ContainerResponse> apply(ContainerRequest requestContext);
/**
* Process a container request with entity stream and return response asynchronously.
* @param requestContext Container request to process
* @param entityStream Output stream for writing response entity
* @return Future containing the container response
*/
public Future<ContainerResponse> apply(ContainerRequest requestContext, OutputStream entityStream);
/**
* Get the application configuration.
* @return Current application configuration
*/
public Configuration getConfiguration();
/**
* Get the injection manager instance.
* @return Injection manager for dependency injection
*/
public InjectionManager getInjectionManager();
/**
* Get the resource model for the application.
* @return Resource model containing all registered resources
*/
public ResourceModel getResourceModel();
}Usage Examples:
import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ContainerRequest;
import java.net.URI;
import java.util.concurrent.Future;
// Create application handler from ResourceConfig
ResourceConfig config = new ResourceConfig()
.packages("com.example.resources");
ApplicationHandler handler = new ApplicationHandler(config);
// Create application handler from Application class
ApplicationHandler handler2 = new ApplicationHandler(MyApplication.class);
// Process a request
URI baseUri = URI.create("http://localhost:8080/api/");
URI requestUri = URI.create("http://localhost:8080/api/users/123");
ContainerRequest request = new ContainerRequest(
baseUri, requestUri, "GET", null, new MapPropertiesDelegate()
);
// Process asynchronously
Future<ContainerResponse> responseFuture = handler.apply(request);
ContainerResponse response = responseFuture.get();
// Access configuration and resource model
Configuration config = handler.getConfiguration();
ResourceModel model = handler.getResourceModel();Server-side request representation providing access to all request information including headers, parameters, entity, security context, and URI information.
/**
* Jersey container request implementation providing server-side HTTP request representation.
* Extends InboundMessageContext and implements ContainerRequestContext.
*/
public class ContainerRequest extends InboundMessageContext implements ContainerRequestContext {
/**
* Create a new container request.
* @param baseUri Application base URI
* @param requestUri Full request URI
* @param httpMethod HTTP method (GET, POST, etc.)
* @param securityContext Security context for the request
* @param propertiesDelegate Properties delegate for request properties
*/
public ContainerRequest(URI baseUri, URI requestUri, String httpMethod,
SecurityContext securityContext, PropertiesDelegate propertiesDelegate);
// URI Information
/**
* Get the base URI of the application.
* @return Base URI
*/
public URI getBaseUri();
/**
* Get the full request URI.
* @return Request URI
*/
public URI getRequestUri();
/**
* Get the absolute request URI builder.
* @return UriBuilder for the absolute request URI
*/
public UriBuilder getAbsolutePathBuilder();
// HTTP Method and Headers
/**
* Get the HTTP method of the request.
* @return HTTP method string
*/
public String getMethod();
/**
* Set the HTTP method of the request.
* @param method HTTP method to set
*/
public void setMethod(String method);
/**
* Get request headers.
* @return MultivaluedMap of request headers
*/
public MultivaluedMap<String, String> getHeaders();
// Security Context
/**
* Get the security context for the request.
* @return Security context
*/
public SecurityContext getSecurityContext();
/**
* Set the security context for the request.
* @param context Security context to set
*/
public void setSecurityContext(SecurityContext context);
// Request Entity
/**
* Check if the request has an entity.
* @return true if request has entity, false otherwise
*/
public boolean hasEntity();
/**
* Get the request entity input stream.
* @return InputStream for reading request entity
*/
public InputStream getEntityStream();
/**
* Set the request entity input stream.
* @param input InputStream containing request entity
*/
public void setEntityStream(InputStream input);
}Usage Examples:
import org.glassfish.jersey.server.ContainerRequest;
import jakarta.ws.rs.core.SecurityContext;
import jakarta.ws.rs.core.UriBuilder;
import java.io.InputStream;
import java.net.URI;
// Create container request
URI baseUri = URI.create("http://localhost:8080/api/");
URI requestUri = URI.create("http://localhost:8080/api/users/123?active=true");
SecurityContext securityContext = createSecurityContext();
PropertiesDelegate propertiesDelegate = new MapPropertiesDelegate();
ContainerRequest request = new ContainerRequest(
baseUri, requestUri, "GET", securityContext, propertiesDelegate
);
// Access request information
String method = request.getMethod(); // "GET"
URI base = request.getBaseUri(); // http://localhost:8080/api/
URI full = request.getRequestUri(); // http://localhost:8080/api/users/123?active=true
// Access headers
MultivaluedMap<String, String> headers = request.getHeaders();
String contentType = headers.getFirst("Content-Type");
String userAgent = headers.getFirst("User-Agent");
// Access security context
SecurityContext security = request.getSecurityContext();
Principal principal = security.getUserPrincipal();
boolean isSecure = security.isSecure();
// Handle request entity
if (request.hasEntity()) {
InputStream entityStream = request.getEntityStream();
// Process entity data
}
// Modify request
request.setMethod("POST");
request.setSecurityContext(newSecurityContext);Server-side response representation providing control over response status, headers, and entity for HTTP responses.
/**
* Jersey container response implementation providing server-side HTTP response representation.
* Implements ContainerResponseContext interface.
*/
public class ContainerResponse implements ContainerResponseContext {
/**
* Create a new container response.
* @param requestContext Associated request context
* @param response JAX-RS Response object
*/
public ContainerResponse(ContainerRequest requestContext, Response response);
// Status Information
/**
* Get the response status code.
* @return HTTP status code
*/
public int getStatus();
/**
* Set the response status code.
* @param code HTTP status code to set
*/
public void setStatus(int code);
/**
* Get the response status info.
* @return Response.StatusType containing status information
*/
public Response.StatusType getStatusInfo();
/**
* Set the response status info.
* @param statusInfo Status information to set
*/
public void setStatusInfo(Response.StatusType statusInfo);
// Headers
/**
* Get response headers.
* @return MultivaluedMap of response headers
*/
public MultivaluedMap<String, Object> getHeaders();
/**
* Get response string headers.
* @return MultivaluedMap of response headers as strings
*/
public MultivaluedMap<String, String> getStringHeaders();
/**
* Get header value as string.
* @param name Header name
* @return Header value as string
*/
public String getHeaderString(String name);
// Entity
/**
* Check if response has an entity.
* @return true if response has entity, false otherwise
*/
public boolean hasEntity();
/**
* Get the response entity.
* @return Response entity object
*/
public Object getEntity();
/**
* Set the response entity.
* @param entity Entity to set
*/
public void setEntity(Object entity);
/**
* Get the response entity output stream.
* @return OutputStream for writing response entity
*/
public OutputStream getEntityStream();
/**
* Set the response entity output stream.
* @param outputStream OutputStream to set
*/
public void setEntityStream(OutputStream outputStream);
// Associated Request
/**
* Get the associated container request.
* @return ContainerRequest that generated this response
*/
public ContainerRequest getRequestContext();
}Usage Examples:
import org.glassfish.jersey.server.ContainerResponse;
import org.glassfish.jersey.server.ContainerRequest;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.MediaType;
import java.io.OutputStream;
// Create response from request and JAX-RS Response
ContainerRequest request = createContainerRequest();
Response jaxrsResponse = Response.ok("Hello World")
.type(MediaType.TEXT_PLAIN)
.header("Custom-Header", "custom-value")
.build();
ContainerResponse response = new ContainerResponse(request, jaxrsResponse);
// Access response information
int status = response.getStatus(); // 200
Response.StatusType statusInfo = response.getStatusInfo(); // OK
// Access headers
MultivaluedMap<String, Object> headers = response.getHeaders();
String contentType = response.getHeaderString("Content-Type"); // "text/plain"
String customHeader = response.getHeaderString("Custom-Header"); // "custom-value"
// Access entity
if (response.hasEntity()) {
Object entity = response.getEntity(); // "Hello World"
}
// Modify response
response.setStatus(201);
response.setStatusInfo(Response.Status.CREATED);
response.getHeaders().add("Location", "/api/users/123");
// Set new entity
response.setEntity("Updated response content");
// Access entity stream for direct writing
OutputStream entityStream = response.getEntityStream();
entityStream.write("Direct response content".getBytes());Container-level exception handling for Jersey server processing errors.
/**
* Container exception representing errors in container-level processing.
* Extends ProcessingException for Jersey-specific error handling.
*/
public class ContainerException extends ProcessingException {
/**
* Create a new container exception.
* @param message Exception message
*/
public ContainerException(String message);
/**
* Create a new container exception with cause.
* @param message Exception message
* @param cause Underlying cause
*/
public ContainerException(String message, Throwable cause);
/**
* Create a new container exception with cause.
* @param cause Underlying cause
*/
public ContainerException(Throwable cause);
}Usage Examples:
import org.glassfish.jersey.server.ContainerException;
// Handle container exceptions in processing
try {
Future<ContainerResponse> responseFuture = handler.apply(request);
ContainerResponse response = responseFuture.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof ContainerException) {
ContainerException containerEx = (ContainerException) e.getCause();
logger.error("Container processing error: " + containerEx.getMessage(), containerEx);
}
}
// Throw container exceptions in custom components
public class CustomContainerProvider implements ContainerProvider {
@Override
public <T> T createContainer(Class<T> type, Application application) {
try {
// Container creation logic
return createContainerInstance(type, application);
} catch (Exception e) {
throw new ContainerException("Failed to create container", e);
}
}
}Extended interfaces providing additional functionality beyond standard JAX-RS contexts.
/**
* Extended resource context providing additional resource-related functionality.
*/
public interface ExtendedResourceContext extends ResourceContext {
/**
* Get resource instance with specific type.
* @param resourceClass Resource class type
* @return Resource instance
*/
<T> T getResource(Class<T> resourceClass);
/**
* Initialize resource instance.
* @param resource Resource instance to initialize
* @return Initialized resource instance
*/
<T> T initResource(T resource);
}
/**
* Extended URI info providing additional URI-related functionality.
*/
public interface ExtendedUriInfo extends UriInfo {
/**
* Get throwable mapped to response.
* @return Mapped throwable or null
*/
Throwable getMappedThrowable();
/**
* Get all template parameter values.
* @return MultivaluedMap of all template parameters
*/
MultivaluedMap<String, String> getPathParameters();
/**
* Get matched resource method.
* @return ResourceMethod that matched the request
*/
ResourceMethod getMatchedResourceMethod();
}Install with Tessl CLI
npx tessl i tessl/maven-org-glassfish-jersey-core--jersey-server