Package org.springframework.web.server.adapter

# Class WebHttpHandlerBuilder

java.lang.Object
org.springframework.web.server.adapter.WebHttpHandlerBuilder

public final class WebHttpHandlerBuilder extends Object
This builder has two purposes:

One is to assemble a processing chain that consists of a target `WebHandler`, then decorated with a set of `WebFilters`, then further decorated with a set of `WebExceptionHandlers`.

The second purpose is to adapt the resulting processing chain to an `HttpHandler`: the lowest-level reactive HTTP handling abstraction which can then be used with any of the supported runtimes. The adaptation is done with the help of `HttpWebHandlerAdapter`.

The processing chain can be assembled manually via builder methods, or detected from a Spring `ApplicationContext` via `applicationContext`, or a mix of both.

Since:
  5.0
Author:
  Rossen Stoyanchev, Sebastien Deleuze
See Also:
  - `HttpWebHandlerAdapter`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`WebHttpHandlerBuilder.SpringWebBlockHoundIntegration`
`BlockHoundIntegration` for spring-web classes.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`FORWARDED_HEADER_TRANSFORMER_BEAN_NAME`
Well-known name for the ForwardedHeaderTransformer in the bean factory.
`static final String`
`LOCALE_CONTEXT_RESOLVER_BEAN_NAME`
Well-known name for the LocaleContextResolver in the bean factory.
`static final String`
`SERVER_CODEC_CONFIGURER_BEAN_NAME`
Well-known name for the ServerCodecConfigurer in the bean factory.
`static final String`
`WEB_HANDLER_BEAN_NAME`
Well-known name for the target WebHandler in the bean factory.
`static final String`
`WEB_SESSION_MANAGER_BEAN_NAME`
Well-known name for the WebSessionManager in the bean factory.

## Method Summary

Modifier and Type
Method
Description
`static WebHttpHandlerBuilder`
`applicationContext(org.springframework.context.ApplicationContext context)`
Static factory method to create a new builder instance by detecting beans in an `ApplicationContext`.
`HttpHandler`
`build()`
Build the `HttpHandler`.
`WebHttpHandlerBuilder`
`clone()`
Clone this `WebHttpHandlerBuilder`.
`WebHttpHandlerBuilder`
`codecConfigurer(ServerCodecConfigurer codecConfigurer)`
Configure the `ServerCodecConfigurer` to set on the `WebServerExchange`.
`WebHttpHandlerBuilder`
`exceptionHandler(WebExceptionHandler... handlers)`
Add the given exception handler(s).
`WebHttpHandlerBuilder`
`exceptionHandlers(Consumer<List<WebExceptionHandler>> consumer)`
Manipulate the "live" list of currently configured exception handlers.
`WebHttpHandlerBuilder`
`filter(WebFilter... filters)`
Add the given filter(s).
`WebHttpHandlerBuilder`
`filters(Consumer<List<WebFilter>> consumer)`
Manipulate the "live" list of currently configured filters.
`WebHttpHandlerBuilder`
`forwardedHeaderTransformer(ForwardedHeaderTransformer transformer)`
Configure the `ForwardedHeaderTransformer` for extracting and/or removing forwarded headers.
`boolean`
`hasCodecConfigurer()`
Whether a `ServerCodecConfigurer` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `codecConfigurer`.
`boolean`
`hasForwardedHeaderTransformer()`
Whether a `ForwardedHeaderTransformer` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `forwardedHeaderTransformer(ForwardedHeaderTransformer)`.
`boolean`
`hasHttpHandlerDecorator()`
Whether a decorator for `HttpHandler` is configured or not via `httpHandlerDecorator(Function)`.
`boolean`
`hasLocaleContextResolver()`
Whether a `LocaleContextResolver` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `localeContextResolver`.
`boolean`
`hasSessionManager()`
Whether a `WebSessionManager` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `sessionManager`.
`WebHttpHandlerBuilder`
`httpHandlerDecorator(Function<HttpHandler,HttpHandler> handlerDecorator)`
Configure a `Function` to decorate the `HttpHandler` returned by this builder which effectively wraps the entire `WebExceptionHandler` - `WebFilter` - `WebHandler` processing chain.
`WebHttpHandlerBuilder`
`localeContextResolver(LocaleContextResolver localeContextResolver)`
Configure the `LocaleContextResolver` to set on the `WebServerExchange`.
`WebHttpHandlerBuilder`
`observationConvention(ServerRequestObservationConvention observationConvention)`
Configure a `ServerRequestObservationConvention` to use for server observations.
`WebHttpHandlerBuilder`
`observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)`
Configure an `ObservationRegistry` for recording server exchange observations.
`WebHttpHandlerBuilder`
`sessionManager(WebSessionManager manager)`
Configure the `WebSessionManager` to set on the `WebServerExchange`.
`static WebHttpHandlerBuilder`
`webHandler(WebHandler webHandler)`
Static factory method to create a new builder instance.

### Methods inherited from class java.lang.Object

`equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### WEB_HANDLER_BEAN_NAME

public static final String WEB_HANDLER_BEAN_NAME
Well-known name for the target WebHandler in the bean factory.

See Also:
  - Constant Field Values

### WEB_SESSION_MANAGER_BEAN_NAME

public static final String WEB_SESSION_MANAGER_BEAN_NAME
Well-known name for the WebSessionManager in the bean factory.

See Also:
  - Constant Field Values

### SERVER_CODEC_CONFIGURER_BEAN_NAME

public static final String SERVER_CODEC_CONFIGURER_BEAN_NAME
Well-known name for the ServerCodecConfigurer in the bean factory.

See Also:
  - Constant Field Values

### LOCALE_CONTEXT_RESOLVER_BEAN_NAME

public static final String LOCALE_CONTEXT_RESOLVER_BEAN_NAME
Well-known name for the LocaleContextResolver in the bean factory.

See Also:
  - Constant Field Values

### FORWARDED_HEADER_TRANSFORMER_BEAN_NAME

public static final String FORWARDED_HEADER_TRANSFORMER_BEAN_NAME
Well-known name for the ForwardedHeaderTransformer in the bean factory.

See Also:
  - Constant Field Values

## Method Details

### webHandler

public static WebHttpHandlerBuilder webHandler(WebHandler webHandler)
Static factory method to create a new builder instance.

Parameters:
  `webHandler` - the target handler for the request
Returns:
  the prepared builder

### applicationContext

public static WebHttpHandlerBuilder applicationContext(org.springframework.context.ApplicationContext context)
Static factory method to create a new builder instance by detecting beans in an `ApplicationContext`. The following are detected:

- `WebHandler` [1] -- looked up by the name `WEB_HANDLER_BEAN_NAME`.
- `WebFilter` [0..N] -- detected by type and ordered, see `AnnotationAwareOrderComparator`.
- `WebExceptionHandler` [0..N] -- detected by type and ordered.
- `HttpHandlerDecoratorFactory` [0..N] -- detected by type and ordered.
- `ObservationRegistry` -- detected by type and configured if unique.
- `ServerRequestObservationConvention` -- detected by type and configured if unique.
- `WebSessionManager` [0..1] -- looked up by the name `WEB_SESSION_MANAGER_BEAN_NAME`.
- `ServerCodecConfigurer` [0..1] -- looked up by the name `SERVER_CODEC_CONFIGURER_BEAN_NAME`.
- `LocaleContextResolver` [0..1] -- looked up by the name `LOCALE_CONTEXT_RESOLVER_BEAN_NAME`.

Parameters:
  `context` - the application context to use for the lookup
Returns:
  the prepared builder

### filter

public WebHttpHandlerBuilder filter(WebFilter... filters)
Add the given filter(s).

Parameters:
  `filters` - the filter(s) to add that's

### filters

public WebHttpHandlerBuilder filters(Consumer<List<WebFilter>> consumer)
Manipulate the "live" list of currently configured filters.

Parameters:
  `consumer` - the consumer to use

### exceptionHandler

public WebHttpHandlerBuilder exceptionHandler(WebExceptionHandler... handlers)
Add the given exception handler(s).

Parameters:
  `handlers` - the exception handler(s)

### exceptionHandlers

public WebHttpHandlerBuilder exceptionHandlers(Consumer<List<WebExceptionHandler>> consumer)
Manipulate the "live" list of currently configured exception handlers.

Parameters:
  `consumer` - the consumer to use

### sessionManager

public WebHttpHandlerBuilder sessionManager(WebSessionManager manager)
Configure the `WebSessionManager` to set on the `WebServerExchange`.

By default `DefaultWebSessionManager` is used.

Parameters:
  `manager` - the session manager
See Also:
  - `HttpWebHandlerAdapter.setSessionManager(WebSessionManager)`

### hasSessionManager

public boolean hasSessionManager()
Whether a `WebSessionManager` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `sessionManager`.

Since:
  5.0.9

### codecConfigurer

public WebHttpHandlerBuilder codecConfigurer(ServerCodecConfigurer codecConfigurer)
Configure the `ServerCodecConfigurer` to set on the `WebServerExchange`.

Parameters:
  `codecConfigurer` - the codec configurer

### hasCodecConfigurer

public boolean hasCodecConfigurer()
Whether a `ServerCodecConfigurer` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `codecConfigurer`.

Since:
  5.0.9

### localeContextResolver

public WebHttpHandlerBuilder localeContextResolver(LocaleContextResolver localeContextResolver)
Configure the `LocaleContextResolver` to set on the `WebServerExchange`.

Parameters:
  `localeContextResolver` - the locale context resolver

### hasLocaleContextResolver

public boolean hasLocaleContextResolver()
Whether a `LocaleContextResolver` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `localeContextResolver`.

Since:
  5.0.9

### forwardedHeaderTransformer

public WebHttpHandlerBuilder forwardedHeaderTransformer(ForwardedHeaderTransformer transformer)
Configure the `ForwardedHeaderTransformer` for extracting and/or removing forwarded headers.

Parameters:
  `transformer` - the transformer
Since:
  5.1

### hasForwardedHeaderTransformer

public boolean hasForwardedHeaderTransformer()
Whether a `ForwardedHeaderTransformer` is configured or not, either detected from an `ApplicationContext` or explicitly configured via `forwardedHeaderTransformer(ForwardedHeaderTransformer)`.

Since:
  5.1

### observationRegistry

public WebHttpHandlerBuilder observationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)
Configure an `ObservationRegistry` for recording server exchange observations. By default, a `no-op` registry will be configured.

Parameters:
  `observationRegistry` - the observation registry
Since:
  6.1

### observationConvention

public WebHttpHandlerBuilder observationConvention(ServerRequestObservationConvention observationConvention)
Configure a `ServerRequestObservationConvention` to use for server observations. By default, a `DefaultServerRequestObservationConvention` will be used.

Parameters:
  `observationConvention` - the convention to use for all recorded observations
Since:
  6.1

### httpHandlerDecorator

public WebHttpHandlerBuilder httpHandlerDecorator(Function<HttpHandler,HttpHandler> handlerDecorator)
Configure a `Function` to decorate the `HttpHandler` returned by this builder which effectively wraps the entire `WebExceptionHandler` - `WebFilter` - `WebHandler` processing chain. This provides access to the request and response before the entire chain and likewise the ability to observe the result of the entire chain.

Parameters:
  `handlerDecorator` - the decorator to apply
Since:
  5.3

### hasHttpHandlerDecorator

public boolean hasHttpHandlerDecorator()
Whether a decorator for `HttpHandler` is configured or not via `httpHandlerDecorator(Function)`.

Since:
  5.3

### build

public HttpHandler build()
Build the `HttpHandler`.

### clone

public WebHttpHandlerBuilder clone()
Clone this `WebHttpHandlerBuilder`.

Overrides:
  `clone` in class `Object`
Returns:
  the cloned builder instance