Package org.springframework.web.reactive.result
支持各种编程模型样式,包括调用不同类型的handles
HandlerResultHandlerSupport
HandlerResultHandler的基类,支持内容协商和访问ReactiveAdapter注册表。
public abstract class HandlerResultHandlerSupport implements Ordered {
private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");
private final RequestedContentTypeResolver contentTypeResolver;
private final ReactiveAdapterRegistry adapterRegistry;
private int order = LOWEST_PRECEDENCE;
protected HandlerResultHandlerSupport(RequestedContentTypeResolver contentTypeResolver,
ReactiveAdapterRegistry adapterRegistry) {
Assert.notNull(contentTypeResolver, "RequestedContentTypeResolver is required");
Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required");
this.contentTypeResolver = contentTypeResolver;
this.adapterRegistry = adapterRegistry;
}
/**
* Return the configured {@link ReactiveAdapterRegistry}.
*/
public ReactiveAdapterRegistry getAdapterRegistry() {
return this.adapterRegistry;
}
/**
* Return the configured {@link RequestedContentTypeResolver}.
*/
public RequestedContentTypeResolver getContentTypeResolver() {
return this.contentTypeResolver;
}
/**
* Set the order for this result handler relative to others.
* By default set to {@link Ordered#LOWEST_PRECEDENCE}, however see
* Javadoc of sub-classes which may change this default.
* @param order the order
*/
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return this.order;
}
/**
* Get a {@code ReactiveAdapter} for the top-level return value type.
* @return the matching adapter or {@code null}
*/
@Nullable
protected ReactiveAdapter getAdapter(HandlerResult result) {
Class> returnType = result.getReturnType().getRawClass();
return getAdapterRegistry().getAdapter(returnType, result.getReturnValue());
}
/**
* Select the best media type for the current request through a content
* negotiation algorithm.
* @param exchange the current request
* @param producibleTypesSupplier the media types that can be produced for the current request
* @return the selected media type or {@code null}
*/
@Nullable
protected MediaType selectMediaType(ServerWebExchange exchange,
Supplier> producibleTypesSupplier) {
List acceptableTypes = getAcceptableTypes(exchange);
List producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier);
Set compatibleMediaTypes = new LinkedHashSet<>();
for (MediaType acceptable : acceptableTypes) {
for (MediaType producible : producibleTypes) {
if (acceptable.isCompatibleWith(producible)) {
compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible));
}
}
}
List result = new ArrayList<>(compatibleMediaTypes);
MediaType.sortBySpecificityAndQuality(result);
for (MediaType mediaType : result) {
if (mediaType.isConcrete()) {
return mediaType;
}
else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
return MediaType.APPLICATION_OCTET_STREAM;
}
}
return null;
}
private List getAcceptableTypes(ServerWebExchange exchange) {
List mediaTypes = getContentTypeResolver().resolveMediaTypes(exchange);
return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
}
@SuppressWarnings("unchecked")
private List getProducibleTypes(ServerWebExchange exchange,
Supplier> producibleTypesSupplier) {
Set mediaTypes = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
return (mediaTypes != null ? new ArrayList<>(mediaTypes) : producibleTypesSupplier.get());
}
private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
producible = producible.copyQualityValue(acceptable);
Comparator comparator = MediaType.SPECIFICITY_COMPARATOR;
return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
}
}
接口 View的Diagram
View
通过视图解析支持结果处理。
将HandlerResult呈现给HTTP响应的约定。
与Encoder相比,Encoder是一个单实例对象,并对给定类型的任何对象进行编码,因此,视图通常是通过名称来选择的,并使用ViewResolver来解析,例如将其与HTML模板匹配。此外,视图可以基于模型中包含的多个属性呈现。
视图还可以选择从模型中选择一个属性,使用任何现有的编码器来呈现替代媒体类型。
返回此视图支持的媒体类型列表,或空列表。
List getSupportedMediaTypes();
这个视图是否通过执行重定向来呈现。
default boolean isRedirectView() {
return false;
}
根据给定的HandlerResult呈现视图。实现可以访问和使用模型,或者仅在其中使用一个特定的属性。
Mono render(@Nullable Map model, @Nullable MediaType contentType, ServerWebExchange exchange);
AbstractView
View实现的基类。
public abstract class AbstractView implements View, ApplicationContextAware {
/** 在 bean factory 有名的RequestDataValueProcessor */
public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";
/** 可用于子类的日志记录器 */
protected final Log logger = LogFactory.getLog(getClass());
private static final Object NO_VALUE = new Object();
private final List mediaTypes = new ArrayList<>(4);
private final ReactiveAdapterRegistry adapterRegistry;
private Charset defaultCharset = StandardCharsets.UTF_8;
@Nullable
private String requestContextAttribute;
@Nullable
private ApplicationContext applicationContext;
public AbstractView() {
this(new ReactiveAdapterRegistry());
}
public AbstractView(ReactiveAdapterRegistry registry) {
this.mediaTypes.add(ViewResolverSupport.DEFAULT_CONTENT_TYPE);
this.adapterRegistry = registry;
}
/**
* Set the supported media types for this view.
* Default is "text/html;charset=UTF-8".
*/
public void setSupportedMediaTypes(@Nullable List supportedMediaTypes) {
Assert.notEmpty(supportedMediaTypes, "MediaType List must not be empty");
this.mediaTypes.clear();
if (supportedMediaTypes != null) {
this.mediaTypes.addAll(supportedMediaTypes);
}
}
/**
* Return the configured media types supported by this view.
*/
@Override
public List getSupportedMediaTypes() {
return this.mediaTypes;
}
/**
* Set the default charset for this view, used when the
* {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
* Default is {@linkplain StandardCharsets#UTF_8 UTF 8}.
*/
public void setDefaultCharset(Charset defaultCharset) {
Assert.notNull(defaultCharset, "'defaultCharset' must not be null");
this.defaultCharset = defaultCharset;
}
/**
* Return the default charset, used when the
* {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
*/
public Charset getDefaultCharset() {
return this.defaultCharset;
}
/**
* Set the name of the RequestContext attribute for this view.
* Default is none.
*/
public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
this.requestContextAttribute = requestContextAttribute;
}
/**
* Return the name of the RequestContext attribute, if any.
*/
@Nullable
public String getRequestContextAttribute() {
return this.requestContextAttribute;
}
@Override
public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Nullable
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
/**
* Obtain the ApplicationContext for actual use.
* @return the ApplicationContext (never {@code null})
* @throws IllegalStateException in case of no ApplicationContext set
*/
protected final ApplicationContext obtainApplicationContext() {
ApplicationContext applicationContext = getApplicationContext();
Assert.state(applicationContext != null, "No ApplicationContext");
return applicationContext;
}
/**
* Prepare the model to render.
* @param model Map with name Strings as keys and corresponding model
* objects as values (Map can also be {@code null} in case of empty model)
* @param contentType the content type selected to render with which should
* match one of the {@link #getSupportedMediaTypes() supported media types}.
* @param exchange the current exchange
* @return {@code Mono} to represent when and if rendering succeeds
*/
@Override
public Mono render(@Nullable Map model, @Nullable MediaType contentType,
ServerWebExchange exchange) {
if (logger.isTraceEnabled()) {
logger.trace("Rendering view with model " + model);
}
if (contentType != null) {
exchange.getResponse().getHeaders().setContentType(contentType);
}
return getModelAttributes(model, exchange).flatMap(mergedModel -> {
// Expose RequestContext?
if (this.requestContextAttribute != null) {
mergedModel.put(this.requestContextAttribute, createRequestContext(exchange, mergedModel));
}
return renderInternal(mergedModel, contentType, exchange);
});
}
/**
* Prepare the model to use for rendering.
* The default implementation creates a combined output Map that includes
* model as well as static attributes with the former taking precedence.
*/
protected Mono
ViewResolver
Rendering
SimpleHandlerAdapter
HandlerAdapter,它允许使用普通的WebHandler与一般的DispatcherHandler一起使用。
public class SimpleHandlerAdapter implements HandlerAdapter {
private static final MethodParameter RETURN_TYPE;
static {
try {
Method method = WebHandler.class.getMethod("handle", ServerWebExchange.class);
RETURN_TYPE = new MethodParameter(method, -1);
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException(
"Failed to initialize the return type for WebHandler: " + ex.getMessage());
}
}
//这个HandlerAdapter是否支持给定的handler。
@Override
public boolean supports(Object handler) {
return WebHandler.class.isAssignableFrom(handler.getClass());
}
//鼓励实现处理由调用handler所产生的异常,并在必要时返回代表错误响应的替代结果。
@Override
public Mono handle(ServerWebExchange exchange, Object handler) {
WebHandler webHandler = (WebHandler) handler;
Mono mono = webHandler.handle(exchange);
return mono.then(Mono.empty());
}
}